mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-03 13:14:30 +03:00
Directly using Django's JSONResponse in the Tutorial (#4935)
* directly using Django's JSONResponse * fixed JsonResponse typo * added safe=False to JsonResponse * removed unnecessary 'safe=False' in JsonResponse
This commit is contained in:
parent
8df340908b
commit
7f59ce1ec2
|
@ -216,26 +216,15 @@ It's important to remember that `ModelSerializer` classes don't do anything part
|
||||||
Let's see how we can write some API views using our new Serializer class.
|
Let's see how we can write some API views using our new Serializer class.
|
||||||
For the moment we won't use any of REST framework's other features, we'll just write the views as regular Django views.
|
For the moment we won't use any of REST framework's other features, we'll just write the views as regular Django views.
|
||||||
|
|
||||||
We'll start off by creating a subclass of HttpResponse that we can use to render any data we return into `json`.
|
|
||||||
|
|
||||||
Edit the `snippets/views.py` file, and add the following.
|
Edit the `snippets/views.py` file, and add the following.
|
||||||
|
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse, JsonResponse
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from rest_framework.renderers import JSONRenderer
|
from rest_framework.renderers import JSONRenderer
|
||||||
from rest_framework.parsers import JSONParser
|
from rest_framework.parsers import JSONParser
|
||||||
from snippets.models import Snippet
|
from snippets.models import Snippet
|
||||||
from snippets.serializers import SnippetSerializer
|
from snippets.serializers import SnippetSerializer
|
||||||
|
|
||||||
class JSONResponse(HttpResponse):
|
|
||||||
"""
|
|
||||||
An HttpResponse that renders its content into JSON.
|
|
||||||
"""
|
|
||||||
def __init__(self, data, **kwargs):
|
|
||||||
content = JSONRenderer().render(data)
|
|
||||||
kwargs['content_type'] = 'application/json'
|
|
||||||
super(JSONResponse, self).__init__(content, **kwargs)
|
|
||||||
|
|
||||||
The root of our API is going to be a view that supports listing all the existing snippets, or creating a new snippet.
|
The root of our API is going to be a view that supports listing all the existing snippets, or creating a new snippet.
|
||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
|
@ -246,15 +235,15 @@ The root of our API is going to be a view that supports listing all the existing
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
snippets = Snippet.objects.all()
|
snippets = Snippet.objects.all()
|
||||||
serializer = SnippetSerializer(snippets, many=True)
|
serializer = SnippetSerializer(snippets, many=True)
|
||||||
return JSONResponse(serializer.data)
|
return JsonResponse(serializer.data, safe=False)
|
||||||
|
|
||||||
elif request.method == 'POST':
|
elif request.method == 'POST':
|
||||||
data = JSONParser().parse(request)
|
data = JSONParser().parse(request)
|
||||||
serializer = SnippetSerializer(data=data)
|
serializer = SnippetSerializer(data=data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
serializer.save()
|
||||||
return JSONResponse(serializer.data, status=201)
|
return JsonResponse(serializer.data, status=201)
|
||||||
return JSONResponse(serializer.errors, status=400)
|
return JsonResponse(serializer.errors, status=400)
|
||||||
|
|
||||||
Note that because we want to be able to POST to this view from clients that won't have a CSRF token we need to mark the view as `csrf_exempt`. This isn't something that you'd normally want to do, and REST framework views actually use more sensible behavior than this, but it'll do for our purposes right now.
|
Note that because we want to be able to POST to this view from clients that won't have a CSRF token we need to mark the view as `csrf_exempt`. This isn't something that you'd normally want to do, and REST framework views actually use more sensible behavior than this, but it'll do for our purposes right now.
|
||||||
|
|
||||||
|
@ -272,15 +261,15 @@ We'll also need a view which corresponds to an individual snippet, and can be us
|
||||||
|
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
serializer = SnippetSerializer(snippet)
|
serializer = SnippetSerializer(snippet)
|
||||||
return JSONResponse(serializer.data)
|
return JsonResponse(serializer.data)
|
||||||
|
|
||||||
elif request.method == 'PUT':
|
elif request.method == 'PUT':
|
||||||
data = JSONParser().parse(request)
|
data = JSONParser().parse(request)
|
||||||
serializer = SnippetSerializer(snippet, data=data)
|
serializer = SnippetSerializer(snippet, data=data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
serializer.save()
|
||||||
return JSONResponse(serializer.data)
|
return JsonResponse(serializer.data)
|
||||||
return JSONResponse(serializer.errors, status=400)
|
return JsonResponse(serializer.errors, status=400)
|
||||||
|
|
||||||
elif request.method == 'DELETE':
|
elif request.method == 'DELETE':
|
||||||
snippet.delete()
|
snippet.delete()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user