Search Posts

Category: python

Django REST framework custom format for all out responses

In my project I use DRF as backend and Angular as frontend. Django==1.10 djangorestframework==3.7.1 I need all responses from DRF to be in the following format. { “status”: “”, // 200,400,…..etc “error”: “”, // True, False “data”: [], // data “message”: “” // Success messages } for this i have written a custom viewset and overridden the functions list, detail, create, update class ResponseModelViewSet(viewsets.ModelViewSet): def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) page = self.paginate_queryset(queryset) […]

How can I access the variable in my js file which rendered from flask? [duplicate]

This question already has an answer here: Is it possible to use Angular with the Jinja2 template engine? 2 answers Pass parameter with Python Flask in external Javascript 2 answers In python flask displaying a variable in a .html file by render_template(“index.html”, message=”hello world”) and in index.html file we can access the it {{message}} expression. As my R&D This expression is same for the angular.js for binding $scope variable. But in my .js file I […]

How can I incorporate an angular JS in flask python framework?

Please bare with me as I have a background in mechanical engineering. I’m hosting a small website using flask. I recently came across a nice project called “gamepad HTML5”, it basically shows me all the info about the gamepad connected to my computer. I took the code from github and could make it work in my codepen: https://codepen.io/diamondx911/pen/mBgYgJ But when I try to put it in my html folder it doesn’t work (note that I […]

AngularJs Http get json file working with Django

i’m trying to make an angular http get to load a json file that is in the same location than my controller, both are in the static Django folder (i don’t know if that is wrong). I have one view and one url registered in my urls.py file it just load the index.html template. The issue is when i try to make the http get to “leyes.json” file, the response.data shows me the code of […]

AngularJS ng-view not showing up using $routeProvider templates

Directory Structure: public film – film.html – film-controller.js main – main.html – film-controller.js – app.js – index.html In my index.html file I have: <div ng-view> </div> in the body and ng-app=”myApp” in the starting html tag app.js angular.module(‘myApp’, [‘ngRoute’]).config(config); function config($routeProvider) { $routeProvider.when(‘/’, { templateUrl: ‘main/main.html’, controller: ‘MainController’, controllerAs: ‘vm’ }).when(‘/film/:id’, { templateUrl : ‘film/film.html’, controller: ‘FilmController’, controllerAs: ‘vm’ }).otherwise({ redirectTo : ‘/’ }); } In main.html I have a <h1>Hello {{vm.name}}</h1> line of code […]

Increment integer field dynamically – Django

So I have the following model: from django.db import models class Person(models.Model): name = models.CharField(max_length=254) image_url = models.CharField(max_length=254) title = models.CharField(max_length=254) bio = models.CharField(max_length=20000) vote = models.IntegerField(default=0) def __str__(self): return self.name Where Vote is the attribute that I want to increment by one on every click. The following Views: import json from rest_framework.views import APIView from rest_framework.response import Response from urllib.request import urlopen from .models import Person from .serializers import PersonSerializer from django.views.generic import ListView […]

Django restful api can not get file content

I use Django restful api + angularjs . Now ,I append to use angularjs upload file to server by Django restful api On the web front-end ,The Html is: <input type=”file” data-upload-file id=”uploadInputFile”> The angularjs’s code is : omapp.directive(“uploadFile”,function(httpPostFactory){ return { restrict:”A”, scope:true, link:function(scope,element,attr){ element.bind(‘change’,function(){ var formData = new FormData(); formData.append(‘file’, element[0].files[0]); console.log(formData.get(‘file’)) httpPostFactory(‘/api/uploadfile/’,formData.get(‘file’),function (callback) { console.log(callback); }) }) } }; }); omapp.factory(‘httpPostFactory’, function ($http,$cookies) { return function (file, data, callback) { var req = […]