前言:

本章主要介绍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.

2.Response objects

REST framework 引入了一个新的对象Response,这是一种处理响应的模板可以对内容进行判断并决定返回给客户端正确的内容类型。

return Response(data)  # Renders to content type as requested by the client.

3.Status codes

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>[
{
&quot;id&quot;: 1,
&quot;title&quot;: &quot;&quot;,
&quot;code&quot;: &quot;foo = \&quot;Jason\&quot;\n&quot;,
&quot;linenos&quot;: false,
&quot;language&quot;: &quot;python&quot;,
&quot;style&quot;: &quot;friendly&quot;
},
{
&quot;id&quot;: 2,
&quot;title&quot;: &quot;&quot;,
&quot;code&quot;: &quot;print \&quot;Hello,Jason\&quot;\n&quot;,
&quot;linenos&quot;: false,
&quot;language&quot;: &quot;python&quot;,
&quot;style&quot;: &quot;friendly&quot;
},
{
&quot;id&quot;: 3,
&quot;title&quot;: &quot;&quot;,
&quot;code&quot;: &quot;print \&quot;Hello,Jason\&quot;&quot;,
&quot;linenos&quot;: false,
&quot;language&quot;: &quot;python&quot;,
&quot;style&quot;: &quot;friendly&quot;
}
]</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$

Django restful Framework 之Requests and Response 方法的更多相关文章

  1. django restful framework 一对多方向更新数据库

    目录 django restful framework 序列化 一 . 数据模型: models 二. 序列化: serializers 三, 视图: views 四, 路由: urls 五. 测试 ...

  2. 在django restful framework中设置django model的property

    众所周知,在django的model中,可以某些字段设置@property和setter deleter getter,这样就可以在存入数据的时候进行一些操作,具体原理请参见廖雪峰大神的博客https ...

  3. 4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍

    4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍 天涯明月笙 关注 2018.02.20 19:23* 字数 762 阅读 135 ...

  4. 3- vue django restful framework 打造生鲜超市 - model设计和资源导入

    3- vue django restful framework 打造生鲜超市 - model设计和资源导入 使用Python3.6与Django2.0.2(Django-rest-framework) ...

  5. 7- vue django restful framework 打造生鲜超市 -商品类别数据展示(上)

    Vue+Django REST framework实战 搭建一个前后端分离的生鲜超市网站 Django rtf 完成 商品列表页 并没有将列表页的数据json 与前端的页面展示结合起来 讲解如果将dr ...

  6. django restful framework教程大全

    一. 什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角 ...

  7. 6- vue django restful framework 打造生鲜超市 -完成商品列表页(下)

    Vue+Django REST framework实战 搭建一个前后端分离的生鲜超市网站 Django rtf 完成 商品列表页下 drf中的request和response drf对于django的 ...

  8. Django REST Framework学习——Android使用REST方法访问Diango

    本文更应该叫做Android如何模拟浏览器访问Django服务器后台. 环境为: Android通过HttpClient访问服务器,从Django中获取json数据,解析显示在UI界面上. 问题为: ...

  9. 1- vue django restful framework 打造生鲜超市

    Vue+Django REST framework实战 使用Python3.6与Django2.0.2(Django-rest-framework)以及前端vue开发的前后端分离的商城网站 项目支持支 ...

随机推荐

  1. u盘作为git仓库,完成不同地方的代码同步

    参考网上一篇 "把Git Repository建到U盘上去" 1.我经常有这样的需求,工作.家里需要对同一份代码或文档编辑,并希望在不同地方能同步: 如果是私密性不那么强,可以直接 ...

  2. iOS-多线程的底层实现

    (1)首先回答什么是线程 1个进程要想执行任务,必须得有线程.线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行 (2)什么是多线程 1个进程中可以开启多条线程,每条线程可以并行(同时 ...

  3. JS-textarea限制输入字数

    解决办法: #descrip 是textarea的id,字数小于40: $("#descrip").on('input',function(event) { if ($(" ...

  4. 多线程sshd爆破程序代码

    不多说了,直接上手代码,也没有啥练手的,都是很熟悉的代码,水一篇,方便作为工作的小工具吧.试了一下,配合一个好点的字典,还是可以作为个人小工具使用的. #!/usr/bin/env python # ...

  5. ios sqlite的创建数据库,表,插入查看数据

    iOS sqlite数据库操作.步骤是: 先加入sqlite开发库libsqlite3.dylib, 新建或打开数据库, 创建数据表, 插入数据, 查询数据并打印 1.新建项目sqliteDemo,添 ...

  6. Spring boot:logback文件配置

    resources文件夹下:新建logback-spring.xml文件. 文件内容like: <?xml version="1.0" encoding="UTF- ...

  7. Struts2中的拦截器详解

    exception:异常拦截器,拦截异常aliasservletConfig18nprepare:预备拦截器,这个拦截器就是为了ModelDriven准备对象的,若Action类实现了preparab ...

  8. 关于一个非常非常无语的bug,与君共勉

    今天,哦,不对,是昨天晚上,我花了大概四十分钟去找一个bug,结果还没找到 错误代码" $('#sendAreaInfo').citypicker('reset'); $('#sendAre ...

  9. JUnit4.12 源码分析之TestClass

    1. TestClass // 源码:org.junit.runners.model.TestClass // 该方法主要提供方法校验和注解搜索 public class TestClass impl ...

  10. Java 之设计模式(总述)

    1. 面向对象设计原则 单一职责原则: 一个类只负责一个功能领域中的相应职责 开闭原则: 软件实体应对扩展开放,而对修改关闭; 里氏代换原则: 所有引用基类对象的地方能够透明地使用其子类的对象; 依赖 ...