django-rest-framework

How to use csrf_token in Django RESTful API and React?

不打扰是莪最后的温柔 提交于 2020-05-25 04:55:06
问题 I have previous experience in Django . If add line {csrf_token} in Django templates then Django handles the functionalities of csrf_token . But when I am trying to develop an API using Django REST Framework then I get stuck. How can I add and handle functionalities like csrf_token in API (back end, developed using Django REST Framework ) and React Native/React JS (front end) like Django templates? 回答1: The first step is to get CSRF token which can be retrieved from the Django csrftoken cookie

Get Authenticated user from token in Django Rest Framework

烈酒焚心 提交于 2020-05-24 21:10:54
问题 I am new in Django and I have managed to build a small API using DRF. I have my angular.js client end posting user auth details and DRF returns a token which looks like this: { 'token' : '9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b' } Based on the tutorial, I am supposed to retrieve the details from request.user But I don't know where to do this. I find it confusing since it doesn't give a good example. Anyone with an idea on how go around it? Your input is highly appreciated. Below is the code

Django Rest Framework, hyperlinking a nested relationship

自作多情 提交于 2020-05-24 20:38:10
问题 I've got two models: User and Ticket . Ticket has one User , User has many Tickets I've accomplished that when i go to url /users/1/tickets , i'm getting the list of user's tickets. I want to use hyperlinked relations, and here is what i see in my User model representation: "tickets": [ "http://127.0.0.1:8000/tickets/5/", "http://127.0.0.1:8000/tickets/6/" ] But I want it to be like "tickets": "http://127.0.0.1:8000/users/1/tickets" Is there a way to do that with DRF? The url: url(r'^users/(

Customize queryset in django-filter ModelChoiceFilter (select) and ModelMultipleChoiceFilter (multi-select) menus based on request

瘦欲@ 提交于 2020-05-24 04:37:44
问题 I'm using django-filter in 2 places: My Django Rest Framework API, and in my FilterViews (Django Filter's Generic ListViews.) In the case of my FilterViews I'm showing both select boxes (ModelChoiceFilter) and multi-select boxes (ModelMultipleChoiceFilter) to be filtered on. I need to be able to limit what's in those select and multi-select inputs based on a field inside the request. It's relatively simple to change what's listed as a kwarg in the relevant field in the FilterSet. For example,

FileUploadParser doesn't get the file name

元气小坏坏 提交于 2020-05-23 05:56:06
问题 I just want to create a REST API that receives a file, process it and return some information. My problem is that I am following this example: http://www.django-rest-framework.org/api-guide/parsers/#fileuploadparser And I can't make it work using Postman or curl, I think I am missing something. The parser always gives me these two errors: FileUpload parse error - none of upload handlers can handle the stream Missing filename. Request should include a Content-Disposition header with a filename

Counting problem in django blog: using django ajax

夙愿已清 提交于 2020-05-17 05:58:11
问题 I created a like button for my django blog using ajax but I'm getting an error that its not counting properly, at first its 0 like in a post when i hit like it works 1 like appeared with unlike button but when i hit unlike and like again it gives 2 likes and sometimes when i unlike it show -1 like i think its jQuery problem I'm not an expert in jQuery jQuery $(document).ready(function() { function updateText(btn, newCount, verb) { btn.text(newCount + " " + verb) } $(".like-btn").click

Adding a user to a group Django Rest Framework

余生长醉 提交于 2020-05-17 05:56:51
问题 I want to use an API to create a user and add it to a selected group. But when I execute the POST request with Postman I get an error saying this group already exists, I don't want to make a new group. I just want to add the user to it. POST body This is what I send in the post. { "email": "user@example.com", "username": "Laerie", "first_name": "Laerie", "last_name": "koek", "password": "password", "groups": [{"name":"user"}] } Response I keep getting this error { "groups": [ { "name": [

How to get distance field in REST response?

你。 提交于 2020-05-16 13:57:33
问题 I am using GeoDjango GIS for proximity search result based on latitude and longitude. The model, is quiet simple: class Address(gismodels.Model): # partner = models.ForeignKey(Partner, related_name="partner_addresses", on_delete=models.CASCADE) street = gismodels.CharField(max_length=255) city = gismodels.CharField(max_length=255) postcode = gismodels.CharField(max_length=255, null=True, blank=True, default='0000') country = gismodels.CharField(max_length=255) businesses = gismodels

Why can't Django REST Framework's HyperlinkedModelSerializer form URL?

烂漫一生 提交于 2020-05-15 18:39:27
问题 New to DRF and everything works as long as I don't include 'url' in fields. Here's what I've got: Serializer: class TaskSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Task fields = ('pk', 'short_desc', 'scheduled_date') View Set: class TaskViewSet(viewsets.ReadOnlyModelViewSet): queryset = Task.objects.all().order_by('scheduled_date') serializer_class = TaskSerializer URLs: router = routers.DefaultRouter() router.register(r'tasks', views.TaskViewSet) urlpatterns = [ [

Add multiple models and custom fields to a json response in Django Rest Framework

白昼怎懂夜的黑 提交于 2020-05-15 08:09:15
问题 i'm new into Python/Django programming and i got stuck with something in a personal project that i'm doing. My issue is that i want to return a custom response based on different models of my application, some of the values will come from custom queries and others are part of the models itself. So, i have the following models in my app(some fields were deleted to not make the post too long): class Parking(models.Model): google_id = models.CharField(max_length=100) short_name = models