Search Posts

Category: django

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) […]

Django RestAuth Custom password reset link

I have tried the solutions I found for a similar question but none worked for me. I am using an angular frontend + DRF + Django Rest Auth, for the confirmation url, I was able to override it to point to my frontend by adding a custom adapter that looks liked this, class AccountAdapter(DefaultAccountAdapter): def send_mail(self, template_prefix, email, context): context[‘activate_url’] = settings.URL_FRONT + ‘access/verify-email/’ + context[‘key’] msg = self.render_mail(template_prefix, email, context) msg.send() with URL_FRONT = […]

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 […]

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 = […]