前言:
本章主要介绍REST framework 内置的必要的功能。
Request objects
Response objects
Status codes
Wrapping API views
结合以上方法定义字节的view
如何给在url中定义特定格式及动态路由
1.Request objects
REST framework 引入了一个新的对象Request,其对Django 的HttpRequest 模块功能进行了扩展,提供了更为灵活的request 解析功能,Request对象的的核心功能是 request.data属性,有点类似于request.POST,但是对于web API 更有用。
request.POST # Only handles form data. Only works for 'POST' method.
request.data # Handles arbitrary data. Works for 'POST', 'PUT' and 'PATCH' methods.
REST framework 引入了一个新的对象Response,这是一种处理响应的模板可以对内容进行判断并决定返回给客户端正确的内容类型。
return Response(data) # Renders to content type as requested by the client.
REST framework 引入了status 功能,里边内置了针对各种状态的对应处理模块,比如说HTTP_400_BAD_REQUEST,总之对状态处理更方便了。
4.封装API views
REST framewor 提供两种封装API views 的方法
a. @api_view 针对以函数定义的views进行封装
b. APIView 针对以类定义的views进行封装
这些封装方法提供了一些功能确保你的view中接收了Request实例,并将获取的内容交给response处理,当然也包含权限验证和提交的数据是否符合要求,会返回405 Method Not Allowed
熟悉了以上新特性后,我们来看看如何应用以上方法
有了Response方法后,就不用在使用之前的JSONResponse方法,具体实现方法如下:
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):
"""
List all 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_400_BAD_REQUEST)
利用以上方式定义views,看起来跟舒服了,有木有,并且我们使用了status方法,这样对不同的响应做了更精确的处理
下面是对每个具体的snippet进行定义,代码如下:
@api_view(['GET', 'PUT', 'DELETE'])
def snippet_detail(request, pk):
"""
Retrieve, update or delete a snippet instance.
"""
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)
如果我们的url中出现类似snippets.json ,我们可以通过给views对应的函数添加format参数,实现此功能,示例如下:
def snippet_list(request, format=None): ##
def snippet_detail(request, pk, format=None):
现在我们调整下url中的路由方式,具体如下所示:
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)
下面来看看请求的时候有何变化,结果如下:
localhost:cmdb JasonWang$ http http://127.0.0.1:8000/rest_api/snippets/
^[[3~HTTP/1.0 200 OK
Allow: OPTIONS, GET, POST
Content-Type: application/json
Date: Fri, 11 Nov 2016 09:55:44 GMT
Server: WSGIServer/0.2 CPython/3.5.0
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN [
{
"code": "foo = \"Jason\"\n",
"id": 1,
"language": "python",
"linenos": false,
"style": "friendly",
"title": ""
},
{
"code": "print \"Hello,Jason\"\n",
"id": 2,
"language": "python",
"linenos": false,
"style": "friendly",
"title": ""
},
{
"code": "print \"Hello,Jason\"",
"id": 3,
"language": "python",
"linenos": false,
"style": "friendly",
"title": ""
}
]
localhost:cmdb JasonWang$ http http://127.0.0.1:8000/rest_api/snippets/ Accept:application/json
HTTP/1.0 200 OK
Allow: OPTIONS, GET, POST
Content-Type: application/json
Date: Fri, 11 Nov 2016 09:56:40 GMT
Server: WSGIServer/0.2 CPython/3.5.0
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN [
{
"code": "foo = \"Jason\"\n",
"id": 1,
"language": "python",
"linenos": false,
"style": "friendly",
"title": ""
},
{
"code": "print \"Hello,Jason\"\n",
"id": 2,
"language": "python",
"linenos": false,
"style": "friendly",
"title": ""
},
{
"code": "print \"Hello,Jason\"",
"id": 3,
"language": "python",
"linenos": false,
"style": "friendly",
"title": ""
}
] l
json header请求
localhost:cmdb JasonWang$ http http://127.0.0.1:8000/rest_api/snippets/ Accept:text/html
HTTP/1.0 200 OK
Allow: OPTIONS, GET, POST
Content-Type: text/html; charset=utf-8
Date: Fri, 11 Nov 2016 09:57:15 GMT
Server: WSGIServer/0.2 CPython/3.5.0
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN <!DOCTYPE html>
<html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="robots" content="NONE,NOARCHIVE" /> <title>Snippet List – Django REST framework</title> <link rel="stylesheet" type="text/css" href="/static/rest_framework/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="/static/rest_framework/css/bootstrap-tweaks.css"/> <link rel="stylesheet" type="text/css" href="/static/rest_framework/css/prettify.css"/>
<link rel="stylesheet" type="text/css" href="/static/rest_framework/css/default.css"/> </head> <body class=""> <div class="wrapper"> <div class="navbar navbar-static-top navbar-inverse">
<div class="container">
<span> <a class='navbar-brand' rel="nofollow" href='http://www.django-rest-framework.org'>
Django REST framework
</a> </span>
<ul class="nav navbar-nav pull-right"> <li><a href='/api/api-auth/login/?next=/rest_api/snippets/'>Log in</a></li> </ul>
</div>
</div> <div class="container"> <ul class="breadcrumb"> <li class="active"><a href="/rest_api/snippets/">Snippet List</a></li> </ul> <!-- Content -->
<div id="content"> <form id="get-form" class="pull-right">
<fieldset> <div class="btn-group format-selection">
<a class="btn btn-primary js-tooltip" href="/rest_api/snippets/" rel="nofollow" title="Make a GET request on the Snippet List resource">GET</a> <button class="btn btn-primary dropdown-toggle js-tooltip" data-toggle="dropdown" title="Specify a format for the GET request">
<span class="caret"></span>
</button>
<ul class="dropdown-menu"> <li>
<a class="js-tooltip format-option" href="/rest_api/snippets/?format=json" rel="nofollow" title="Make a GET request on the Snippet List resource with the format set to `json`">json</a>
</li> <li>
<a class="js-tooltip format-option" href="/rest_api/snippets/?format=api" rel="nofollow" title="Make a GET request on the Snippet List resource with the format set to `api`">api</a>
</li> </ul>
</div> </fieldset>
</form> <form class="button-form" action="/rest_api/snippets/" data-method="OPTIONS">
<button class="btn btn-primary js-tooltip" title="Make an OPTIONS request on the Snippet List resource">OPTIONS</button>
</form> <div class="content-main">
<div class="page-header">
<h1>Snippet List</h1>
</div>
<div style="float:left"> <p>List all snippets, or create a new snippet.</p> </div> <div class="request-info" style="clear: both" >
<pre class="prettyprint"><b>GET</b> /rest_api/snippets/</pre>
</div> <div class="response-info">
<pre class="prettyprint"><span class="meta nocode"><b>HTTP 200 OK</b>
<b>Allow:</b> <span class="lit">OPTIONS, GET, POST</span>
<b>Content-Type:</b> <span class="lit">application/json</span>
<b>Vary:</b> <span class="lit">Accept</span> </span>[
{
"id": 1,
"title": "",
"code": "foo = \"Jason\"\n",
"linenos": false,
"language": "python",
"style": "friendly"
},
{
"id": 2,
"title": "",
"code": "print \"Hello,Jason\"\n",
"linenos": false,
"language": "python",
"style": "friendly"
},
{
"id": 3,
"title": "",
"code": "print \"Hello,Jason\"",
"linenos": false,
"language": "python",
"style": "friendly"
}
]</pre>
</div>
</div> <div > <div class="well tab-content"> <div id="post-generic-content-form"> <form action="/rest_api/snippets/" method="POST" class="form-horizontal">
<fieldset> <div class="form-group">
<label for="id__content_type" class="col-sm-2 control-label">Media type:</label>
<div class="col-sm-10">
<select data-override="content-type" id="id__content_type" name="_content_type" required class="form-control">
<option value="application/json" selected="selected">application/json</option>
<option value="application/x-www-form-urlencoded">application/x-www-form-urlencoded</option>
<option value="multipart/form-data">multipart/form-data</option>
</select>
<span class="help-block"></span>
</div>
</div> <div class="form-group">
<label for="id__content" class="col-sm-2 control-label">Content:</label>
<div class="col-sm-10">
<textarea cols="40" data-override="content" id="id__content" name="_content" rows="10" required class="form-control">
</textarea>
<span class="help-block"></span>
</div>
</div> <div class="form-actions">
<button class="btn btn-primary" title="Make a POST request on the Snippet List resource">POST</button>
</div>
</fieldset>
</form> </div>
</div>
</div> </div><!-- /.content -->
</div><!-- /.container -->
</div><!-- ./wrapper --> <script>
window.drf = {
csrfHeaderName: "X-CSRFTOKEN",
csrfCookieName: "csrftoken"
};
</script>
<script src="/static/rest_framework/js/jquery-1.12.4.min.js"></script>
<script src="/static/rest_framework/js/ajax-form.js"></script>
<script src="/static/rest_framework/js/csrf.js"></script>
<script src="/static/rest_framework/js/bootstrap.min.js"></script>
<script src="/static/rest_framework/js/prettify-min.js"></script>
<script src="/static/rest_framework/js/default.js"></script>
<script>
$(document).ready(function() {
$('form').ajaxForm();
});
</script> </body> </html>
html header 请求
发送Post请求,更新数据,方法如下:
localhost:cmdb JasonWang$ http --form POST http://127.0.0.1:8000/rest_api/snippets/ code="print 123"
HTTP/1.0 201 Created
Allow: OPTIONS, GET, POST
Content-Type: application/json
Date: Fri, 11 Nov 2016 09:59:46 GMT
Server: WSGIServer/0.2 CPython/3.5.0
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN {
"code": "print 123",
"id": 4,
"language": "python",
"linenos": false,
"style": "friendly",
"title": ""
} localhost:cmdb JasonWang$