一、请求对象
request.POST # 处理表单数据. 限于POST请求. request.data # 处理任意数据. 对 'POST', 'PUT' and 'PATCH' 方法.
二、响应对象
return Response(data) #根据内容渲染制定类型
三、状态码
REST框架为每个状态码(status code)提供更明确的标识符,例如在状态(status)模型中的HTTP_400_BAD_REQUEST
四、装饰API视图
1.@api_view用于视图的方法上
2.APIView 基于视图类上
snippets/views.py
# -*- coding: utf-8 -*- from __future__ import unicode_literals # Create your views here. from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response from snippets.models import Snippet from snippets.serializers import SnippetSerializer @api_view(["GET", "POST"]) def snippet_list(request, format=None): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': snippets = Snippet.objects.all() serializer = SnippetSerializer(snippets, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = SnippetSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND) @api_view(["GET", "PUT", "DELETE"]) def snippet_detail(request, pk, format=None): """ Retrieve, update or delete a code snippet. """ try: snippet = Snippet.objects.get(pk=pk) except Snippet.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = SnippetSerializer(snippet) return Response(serializer.data) elif request.method == 'PUT': serializer = SnippetSerializer(snippet, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': snippet.delete() return Response(status=status.HTTP_204_NO_CONTENT)
snippets/urls.py
#!/usr/bin/env python # coding:utf-8 from __future__ import absolute_import, print_function, division from django.conf.urls import url from rest_framework.urlpatterns import format_suffix_patterns from snippets import views urlpatterns = [ url(r'^snippets/$', views.snippet_list), url(r'^snippets/(?P<pk>[0-9]+)/$', views.snippet_detail), ] urlpatterns = format_suffix_patterns(urlpatterns)
五、测试
# http http://127.0.0.1:8000/snippets/ HTTP/1.0 200 OK Allow: POST, OPTIONS, GET Content-Length: 225 Content-Type: application/json Date: Fri, 18 Aug 2017 14:58:53 GMT Server: WSGIServer/0.1 Python/2.7.12 Vary: Accept, Cookie X-Frame-Options: SAMEORIGIN [ { "code": "foo = "bar" ", "id": 1, "language": "python", "linenos": false, "style": "friendly", "title": "" }, { "code": "print "Hello, Django-restframework" ", "id": 2, "language": "python", "linenos": false, "style": "friendly", "title": "" } ]
http http://127.0.0.1:8000/snippets/ Accept:application/json # Request JSON http http://127.0.0.1:8000/snippets/ Accept:text/html # Request HTML http http://127.0.0.1:8000/snippets.json # JSON suffix http http://127.0.0.1:8000/snippets.api # Browsable API suffix
# POST using form data http --form POST http://127.0.0.1:8000/snippets/ code="print 123" { "id": 3, "title": "", "code": "print 123", "linenos": false, "language": "python", "style": "friendly" } # POST using JSON http --json POST http://127.0.0.1:8000/snippets/ code="print 456" { "id": 4, "title": "", "code": "print 456", "linenos": false, "language": "python", "style": "friendly" }