Let's look at how the serializer class can write the view function of the API, and now we won't use the attributes in the REST framework, just write the native Django view function.
We create a subclass of HttpResponse to convert any data into JSON format.
Editsnippets/views.py,如下:
from django.http import httpresponsefrom django.views.decorators.csrf import Csrf_exemptfrom rest_framework.renderers import jsonrendererfrom rest_framework.parsers import jsonparserfrom snippets.models import snippetfrom snippets.serializers import snippetserializerclass 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)
Two. The purpose of our API is to use view to enumerate all snippet content, or to create a new snippet
@csrf_exemptdef snippet_list (Request): "" " list all code snippets, or create a new snippet. "" " if request.method == ' GET ': snippets =snippet.objects.all () Serializer =snippetserializer (snippets,many=true) Return jsonresponse (serializer.data) elif request.method == ' POST ': Data =jsonparser (). Parse (Request) Serializer =snippetserializer (Data=data) if serializer.is_valid (): serializer.save () return jsonresponse (serializer.data, status=201) return jsonresponse (Serializer.errors, status=400)
Note that because we are going to post a request to the view through the client, we will label the view as csrf_exempt to indicate that it is not a CSRF event. Unlike the normal view functions of the past, the view function of the rest framework actually uses more sensitive behavior, and it is now only for our purposes.
Three. We also want a view function to serve a separate Snipet instance to restore, update, and delete snippet
@csrf_exemptdef snippet_detail (REQUEST, PK): "" " Retrieve, update or delete a code snippet. "" "try: snippet =snippet.objects.get (PK=PK) except snippet.doesnotexist:return httpresponse (status =404) if request.method == ' GET ': serializer = Snippetserializer (snippet) return jsonresponse (serializer.data) elif request.method == ' PUT ': data = Jsonparser (). Parse (Request) serializer =snippetserializer ( Snippet, data=data) if serializer.is_valid (): & nbsp; serializer.save () return jsonresponse (Serializer.data) return jsonresponse (serializer.errors, status=400) elif request.method == ' DELETE ': snippet.delete ( ) return httpresponse (status=204)
Four. Save the views.py, create the urls.py below the snippets directory, and add the following:
From django.conf.urls import patterns, Urlurlpatterns = Patterns (' snippets.views ', url (r ' ^snippets/$ ', ' snippet_list ') , the URL (r ' ^snippets/(? p<pk>[0-9]+)/$ ', ' Snippet_detail '),)
Four. Testing
Python manage.py runserver
In the other terminal:
Curl http://127.0.0.1:8000/snippets/[{"id": 1, "title": "", "Code": "foo = \" Bar\ "\ n", "Linenos": false, "language": "Pyth On "," style ":" Friendly "}, {" id ": 2," title ":" "," Code ":" Print \ "Hello, world\" \ n "," Linenos ": false," language ": "Python", "style": "Friendly"}]
This article is from the "Automation Rolin" blog, so be sure to keep this source http://luoguoling.blog.51cto.com/1568501/1688166
Django RESTful research A first test example (using a serializer to write a native Django view function)