Django restful Framework 之Requests and Response 方法

前言:css

本章主要介紹REST framework 內置的必要的功能。html

Request objects

Response objects

Status codes

Wrapping API views

結合以上方法定義字節的viewpython

如何給在url中定義特定格式及動態路由jquery

1.Request objects

REST framework 引入了一個新的對象Request,其對Django 的HttpRequest 模塊功能進行了擴展,提供了更爲靈活的request 解析功能,Request對象的的核心功能是 request.data屬性,有點相似於request.POST,可是對於web API 更有用。web

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 objectsajax

REST framework 引入了一個新的對象Response,這是一種處理響應的模板能夠對內容進行判斷並決定返回給客戶端正確的內容類型。django

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

 3.Status codesjson

REST framework 引入了status 功能,裏邊內置了針對各類狀態的對應處理模塊,好比說HTTP_400_BAD_REQUEST,總之對狀態處理更方便了。bootstrap

 4.封裝API viewsapi

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$ 
相關文章
相關標籤/搜索