django DRF


DjangoRestFramework

1. 安裝:

pip install djangorestframework

2. restful規范:

2.1 API與用戶通信協議,總是使用https協議(我們現在用的大部分都是http)

2.2 域名

  https://api.cainiao.com  盡量將API部署在專用域名(會存在跨域問題)

  https://cainiao.com/api/  API很簡單(推薦使用)

2.3 版本

  URL,如:https://api.cainiao.com/v1/

  存在跨域問題咱不慌,請往這里看=>:https://www.cnblogs.com/xingxingnbsp/p/10644135.html

2.4 路徑,將網絡上的所有東西都看成是資源,均使用名詞表示(可以使用復數)

  https://api.cainiao.com/v1/zoos

  https://api.cainiao.com/v1/animls

  https://api.cainiao.com/v1/employees

2.5 method(請求方式)

  GET : 從服務器取出資源(一項或多項)

  POST : 在服務器新建一個資源

  PUT : 在服務器更新資源(客戶端提供改變后的完整資源)

  PATCH : 在服務器更新資源(客戶端提供改變的屬性)

  DELETE : 從服務器刪除資源

2.6 過濾,通過在url上傳參的形式傳遞搜索條件

  https://api.cainiao.com/v1/zoos?limit=10  : 指定返回記錄的數量

  https://api.cainiao.com/v1/zoos?offset=10  : 指定返回記錄的數量

  https://api.cainiao.com/v1/zoos?page=2&per_page=100  : 指定第幾頁,以及每頁的記錄數

  https://api.cainiao.com/v1/zoos?sortby=name&order=asc  : 指定返回結果按照哪個屬性排序,以及排序規則

2.7 狀態碼

  200 OK - [GET]:服務器成功返回用戶請求的數據,該操作是冪等的(Idempotent)。

  201 CREATED - [POST/PUT/PATCH]:用戶新建或修改數據成功。

  202 Accepted - [*]:表示一個請求已經進入后台排隊(異步任務)

  204 NO CONTENT - [DELETE]:用戶刪除數據成功。

  400 INVALID REQUEST - [POST/PUT/PATCH]:用戶發出的請求有錯誤,服務器沒有進行新建或修改數據的操作,該操作是冪等的。

  401 Unauthorized - [*]:表示用戶沒有權限(令牌、用戶名、密碼錯誤)。

  403 Forbidden - [*] 表示用戶得到授權(與401錯誤相對),但是訪問是被禁止的。

  404 NOT FOUND - [*]:用戶發出的請求針對的是不存在的記錄,服務器沒有進行操作,該操作是冪等的。

  406 Not Acceptable - [GET]:用戶請求的格式不可得(比如用戶請求JSON格式,但是只有XML格式)。

  410 Gone -[GET]:用戶請求的資源被永久刪除,且不會再得到的。

  422 Unprocesable entity - [POST/PUT/PATCH] 當創建一個對象時,發生一個驗證錯誤。

  500 INTERNAL SERVER ERROR - [*]:服務器發生錯誤,用戶將無法判斷發出的請求是否成功。

  更多看這里:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

2.8 錯誤處理

{ error:"lnvalid API key" }

 

2.9 返回結果,針對不同操作,服務器向用戶返回的結果應該符合以下規范。

  GET/collection : 返回資源對象的列表(數組)

  GET/collection/response : 返回單個資源對象

  POST/collection : 返回新生成的資源對象

  PUT/collection/response : 返回完整的資源對象

  PATCH/collection/response :返回完整的資源對象

  DELETE/collection/response :返回一個空文檔

2.10 Hypermedia API,RESTful API最好做到Hypermedia,即返回結果中提供鏈接,連向其他API方法,使得用戶不查文檔,也知道下一步應該做什么。

{"link": { "rel":   "collection https://www.cainiao.com/zoos", "href":  "https://api.cainiao.com/zoos", "title": "List of zoos", "type":  "application/vnd.yourformat+json" }}

 3. 配置文件(settings.py)

# django rest framework配置
REST_FRAMEWORK = { # 解析器配置
    'DEFAULT_PARSER_CLASSES':[ 'rest_framework.parsers.JSONParser'
        'rest_framework.parsers.FormParser'
        'rest_framework.parsers.MultiPartParser' ] # 版本配置
    'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.URLPathVersioning", 'DEFAULT_VERSION': 'v1',                # 默認版本
    'ALLOWED_VERSIONS': ['v1', 'v2'],        # 允許的版本
    'VERSION_PARAM': 'version'                 # URL中獲取值的key
    
    # 用戶登錄認證
    'UNAUTHENTICATED_USER': None, 'UNAUTHENTICATED_TOKEN': None, "DEFAULT_AUTHENTICATION_CLASSES": ["web.utils.TestAuthentication",], # 用戶權限驗證
    "DEFAULT_PERMISSION_CLASSES": ["web.utils.TestPermission",], # 用戶訪問頻率限制(匿名時用IP限制+登錄時用Token限制)
   'DEFAULT_THROTTLE_CLASSES': [ 'api.utils.throttles.throttles.LuffyAnonRateThrottle', 'api.utils.throttles.throttles.LuffyUserRateThrottle', ], 'DEFAULT_THROTTLE_RATES': { 'anon': '10/day', 'user': '10/day', 'luffy_anon': '10/m', 'luffy_user': '20/m',
},

4.模塊詳細介紹:

4.1 認證模塊,權限模塊:

4.1.1 用戶url傳入的token認證:

urls.py: from django.conf.urls import url, include from web.viewsimport TestView urlpatterns = [ url(r'^test/', TestView.as_view()), ]
views.py:
from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.authentication import BaseAuthentication from rest_framework.request import Request from rest_framework import exceptions token_list = [ 'sfsfss123kuf3j123', 'asijnfowerkkf9812', ] class TestAuthentication(BaseAuthentication): def authenticate(self, request): """ 注釋001 """ val = request.query_params.get('token') if val not in token_list: raise exceptions.AuthenticationFailed("用戶認證失敗") return ('登錄用戶', '用戶token') def authenticate_header(self, request): # 驗證失敗時,返回的響應頭WWW-Authenticate對應的值 pass class TestView(APIView): authentication_classes = [TestAuthentication, ] permission_classes = [] def get(self, request, *args, **kwargs): print(request.user) print(request.auth) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

4.1.2 請求頭認證:

urls.py: from django.conf.urls import url, include from web.viewsimport TestView urlpatterns = [ url(r'^test/', TestView.as_view()), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.authentication import BaseAuthentication from rest_framework.request import Request from rest_framework import exceptions token_list = [ 'sfsfss123kuf3j123', 'asijnfowerkkf9812', ] class TestAuthentication(BaseAuthentication): def authenticate(self, request): """ 注釋001 """
            import base64 auth = request.META.get('HTTP_AUTHORIZATION', b'') if auth: auth = auth.encode('utf-8') auth = auth.split() if not auth or auth[0].lower() != b'basic': raise exceptions.AuthenticationFailed('驗證失敗') if len(auth) != 2: raise exceptions.AuthenticationFailed('驗證失敗') username, part, password = base64.b64decode(auth[1]).decode('utf-8').partition(':') if username == 'alex' and password == '123': return ('登錄用戶', '用戶token') else: raise exceptions.AuthenticationFailed('用戶名或密碼錯誤') def authenticate_header(self, request): return 'Basic realm=api'
                        
    class TestView(APIView): authentication_classes = [TestAuthentication, ] permission_classes = [] def get(self, request, *args, **kwargs): print(request.user) print(request.auth) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

4.1.3 多個認證規則:

urls.py: from django.conf.urls import url, include from web.views.s2_auth import TestView urlpatterns = [ url(r'^test/', TestView.as_view()), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.authentication import BaseAuthentication from rest_framework.request import Request from rest_framework import exceptions token_list = [ 'sfsfss123kuf3j123', 'asijnfowerkkf9812', ] class Test1Authentication(BaseAuthentication): def authenticate(self, request): """ 注釋001 """
            import base64 auth = request.META.get('HTTP_AUTHORIZATION', b'') if auth: auth = auth.encode('utf-8') else: return None print(auth,'xxxx') auth = auth.split() if not auth or auth[0].lower() != b'basic': raise exceptions.AuthenticationFailed('驗證失敗') if len(auth) != 2: raise exceptions.AuthenticationFailed('驗證失敗') username, part, password = base64.b64decode(auth[1]).decode('utf-8').partition(':') if username == 'alex' and password == '123': return ('登錄用戶', '用戶token') else: raise exceptions.AuthenticationFailed('用戶名或密碼錯誤') def authenticate_header(self, request): # return 'Basic realm=api'
            pass

    class Test2Authentication(BaseAuthentication): def authenticate(self, request): """ 注釋001 """ val = request.query_params.get('token') if val not in token_list: raise exceptions.AuthenticationFailed("用戶認證失敗") return ('登錄用戶', '用戶token') def authenticate_header(self, request): pass

    class TestView(APIView): authentication_classes = [Test1Authentication, Test2Authentication] permission_classes = [] def get(self, request, *args, **kwargs): print(request.user) print(request.auth) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

4.1.4 認證和權限:

 

urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'^test/', TestView.as_view()), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.authentication import BaseAuthentication from rest_framework.permissions import BasePermission from rest_framework.request import Request from rest_framework import exceptions token_list = [ 'sfsfss123kuf3j123', 'asijnfowerkkf9812', ] class TestAuthentication(BaseAuthentication): def authenticate(self, request): """  注釋001 """             val = request.query_params.get('token') if val not in token_list: raise exceptions.AuthenticationFailed("用戶認證失敗") return ('登錄用戶', '用戶token') def authenticate_header(self, request): pass class TestPermission(BasePermission): message = "權限驗證失敗" def has_permission(self, request, view): """  判斷是否有權限訪問當前請求 Return `True` if permission is granted, `False` otherwise. :param request: :param view: :return: True有權限;False無權限 """             if request.user == "管理員": return True # GenericAPIView中get_object時調用 def has_object_permission(self, request, view, obj): """  視圖繼承GenericAPIView,並在其中使用get_object時獲取對象時,觸發單獨 對象權限驗證 Return `True` if permission is granted, `False` otherwise. :param request: :param view: :param obj: :return: True有權限;False無權限 """             if request.user == "管理員": return True class TestView(APIView): # 認證的動作是由request.user觸發 authentication_classes = [TestAuthentication, ] # 權限 # 循環執行所有的權限 permission_classes = [TestPermission, ] def get(self, request, *args, **kwargs): # self.dispatch print(request.user) print(request.auth) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

 

4.1.5 全局使用:

settings.py: REST_FRAMEWORK = { 'UNAUTHENTICATED_USER': None, 'UNAUTHENTICATED_TOKEN': None, "DEFAULT_AUTHENTICATION_CLASSES": [ "web.utils.TestAuthentication", ], "DEFAULT_PERMISSION_CLASSES": [ "web.utils.TestPermission", ], } urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'^test/', TestView.as_view()), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response class TestView(APIView): def get(self, request, *args, **kwargs): # self.dispatch print(request.user) print(request.auth) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

 

 

4.1.6 基於django-rest-framework的JWT認證(推薦使用):

安裝:

pip install djangorestframework-jwt

配置:

REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', #新增 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', ), } JWT_AUTH = {   #JWT_EXPIRATION_DELTA 指明token的有效期 'JWT_EXPIRATION_DELTA':datetime.timedelta(days=1),
}

 

使用:

from rest_framework_jwt.settings import api_settings jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload)

使用Django REST framework JWT進行用戶登錄:

from rest_framework_jwt.views import obtain_jwt_token urlpatterns = [ url(r'^authorizations/$', obtain_jwt_token), ]

4.2 用戶訪問次數/頻率限制:

4.2.1 基於用戶IP限制訪問頻率:

 

urls.py from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'^test/', TestView.as_view()), ] views.py: import time from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import exceptions from rest_framework.throttling import BaseThrottle from rest_framework.settings import api_settings # 保存訪問記錄 RECORD = { '用戶IP': [12312139, 12312135, 12312133, ] } class TestThrottle(BaseThrottle): ctime = time.time def get_ident(self, request): """  根據用戶IP和代理IP,當做請求者的唯一標識 """             xff = request.META.get('HTTP_X_FORWARDED_FOR') remote_addr = request.META.get('REMOTE_ADDR') num_proxies = api_settings.NUM_PROXIES if num_proxies is not None: if num_proxies == 0 or xff is None: return remote_addr addrs = xff.split(',') client_addr = addrs[-min(num_proxies, len(addrs))] return client_addr.strip() return ''.join(xff.split()) if xff else remote_addr def allow_request(self, request, view): """  是否仍然在允許范圍內 Return `True` if the request should be allowed, `False` otherwise. :param request: :param view: :return: True,表示可以通過;False表示已超過限制,不允許訪問 """  # 獲取用戶唯一標識(如:IP) # 允許一分鍾訪問10次 num_request = 10 time_request = 60 now = self.ctime() ident = self.get_ident(request) self.ident = ident if ident not in RECORD: RECORD[ident] = [now, ] return True history = RECORD[ident] while history and history[-1] <= now - time_request: history.pop() if len(history) < num_request: history.insert(0, now) return True def wait(self): """  多少秒后可以允許繼續訪問 Optionally, return a recommended number of seconds to wait before the next request. """             last_time = RECORD[self.ident][0] now = self.ctime() return int(60 + last_time - now) class TestView(APIView): throttle_classes = [TestThrottle, ] def get(self, request, *args, **kwargs): # self.dispatch print(request.user) print(request.auth) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容') def throttled(self, request, wait): """  訪問次數被限制時,定制錯誤信息 """             class Throttled(exceptions.Throttled): default_detail = '請求被限制.' extra_detail_singular = '請 {wait} 秒之后再重試.' extra_detail_plural = '請 {wait} 秒之后再重試.' raise Throttled(wait)

 

 

 

4.2.2 基於用戶IP顯示訪問頻率(利於Django緩存):

settings.py: REST_FRAMEWORK = { 'DEFAULT_THROTTLE_RATES': { 'test_scope': '10/m', }, } urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'^test/', TestView.as_view()), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import exceptions from rest_framework.throttling import SimpleRateThrottle class TestThrottle(SimpleRateThrottle): # 配置文件定義的顯示頻率的Key
        scope = "test_scope"
    
        def get_cache_key(self, request, view): if not request.user: ident = self.get_ident(request) else: ident = request.user return self.cache_format % { 'scope': self.scope, 'ident': ident } class TestView(APIView): throttle_classes = [TestThrottle, ] def get(self, request, *args, **kwargs): # self.dispatch
            print(request.user) print(request.auth) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容') def throttled(self, request, wait): """ 訪問次數被限制時,定制錯誤信息 """
    
            class Throttled(exceptions.Throttled): default_detail = '請求被限制.' extra_detail_singular = '請 {wait} 秒之后再重試.' extra_detail_plural = '請 {wait} 秒之后再重試.'
    
            raise Throttled(wait)

4.2.3 view中限制請求頻率:

settings.py: REST_FRAMEWORK = { 'DEFAULT_THROTTLE_RATES': { 'xxxxxx': '10/m', }, } urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'^test/', TestView.as_view()), ] views.py: # !/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import exceptions from rest_framework.throttling import ScopedRateThrottle # 繼承 ScopedRateThrottle
    class TestThrottle(ScopedRateThrottle): def get_cache_key(self, request, view): """ 應該返回一個惟一的緩存鍵,該緩存鍵可用於節流。 必須覆蓋。 如果請求不應該被節流,可以返回“None”。 """
            if not request.user: ident = self.get_ident(request) else: ident = request.user return self.cache_format % { 'scope': self.scope, 'ident': ident } class TestView(APIView): throttle_classes = [TestThrottle, ] # 在settings中獲取 xxxxxx 對應的頻率限制值
        throttle_scope = "xxxxxx"
    
        def get(self, request, *args, **kwargs): # self.dispatch
            print(request.user) print(request.auth) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容') def throttled(self, request, wait): """ 訪問次數被限制時,定制錯誤信息 """
    
            class Throttled(exceptions.Throttled): default_detail = '請求被限制.' extra_detail_singular = '請 {wait} 秒之后再重試.' extra_detail_plural = '請 {wait} 秒之后再重試.'
    
            raise Throttled(wait)

 

4.3.4 匿名時用IP限制+登錄時用Token限制:

settings.py: REST_FRAMEWORK = { 'UNAUTHENTICATED_USER': None, 'UNAUTHENTICATED_TOKEN': None, 'DEFAULT_THROTTLE_RATES': { 'luffy_anon': '10/m', 'luffy_user': '20/m', }, } urls.py: from django.conf.urls import url, include from web.views.s3_throttling import TestView urlpatterns = [ url(r'^test/', TestView.as_view()), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.throttling import SimpleRateThrottle class LuffyAnonRateThrottle(SimpleRateThrottle): """ 匿名用戶,根據IP進行限制 """ scope = "luffy_anon"
    
        def get_cache_key(self, request, view): # 用戶已登錄,則跳過 匿名頻率限制
            if request.user: return None return self.cache_format % { 'scope': self.scope, 'ident': self.get_ident(request) } class LuffyUserRateThrottle(SimpleRateThrottle): """ 登錄用戶,根據用戶token限制 """ scope = "luffy_user"
    
        def get_ident(self, request): """ 認證成功時:request.user是用戶對象;request.auth是token對象 :param request: :return: """
            # return request.auth.token
            return "user_token"
    
        def get_cache_key(self, request, view): """ 獲取緩存key :param request: :param view: :return: """
            # 未登錄用戶,則跳過 Token限制
            if not request.user: return None return self.cache_format % { 'scope': self.scope, 'ident': self.get_ident(request) } class TestView(APIView): throttle_classes = [LuffyUserRateThrottle, LuffyAnonRateThrottle, ] def get(self, request, *args, **kwargs): # self.dispatch
            print(request.user) print(request.auth) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

 

4.3.5 全局使用

settings.py: REST_FRAMEWORK = { 'DEFAULT_THROTTLE_CLASSES': [ 'api.utils.throttles.throttles.LuffyAnonRateThrottle', 'api.utils.throttles.throttles.LuffyUserRateThrottle', ], 'DEFAULT_THROTTLE_RATES': { 'anon': '10/day', 'user': '10/day', 'luffy_anon': '10/m', 'luffy_user': '20/m', }, }

 

4.3 版本控制:

4.3.1 基於url的get傳參方式(如:/users?version=v1):

settings.py: REST_FRAMEWORK = { 'DEFAULT_VERSION': 'v1',  # 默認版本
        'ALLOWED_VERSIONS': ['v1', 'v2'],  # 允許的版本
        'VERSION_PARAM': 'version'  # URL中獲取值的key
 } urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'^test/', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.versioning import QueryParameterVersioning class TestView(APIView): versioning_class = QueryParameterVersioning def get(self, request, *args, **kwargs): # 獲取版本
            print(request.version) # 獲取版本管理的類
            print(request.versioning_scheme) # 反向生成URL
            reverse_url = request.versioning_scheme.reverse('test', request=request) print(reverse_url) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

4.3.2 基於url的正則方式(如:/v1/users/):

 

settings.py: REST_FRAMEWORK = { 'DEFAULT_VERSION': 'v1',  # 默認版本
        'ALLOWED_VERSIONS': ['v1', 'v2'],  # 允許的版本
        'VERSION_PARAM': 'version'  # URL中獲取值的key
 } urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'^(?P<version>[v1|v2]+)/test/', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.versioning import URLPathVersioning class TestView(APIView): versioning_class = URLPathVersioning def get(self, request, *args, **kwargs): # 獲取版本
            print(request.version) # 獲取版本管理的類
            print(request.versioning_scheme) # 反向生成URL
            reverse_url = request.versioning_scheme.reverse('test', request=request) print(reverse_url) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

 

4.3.3 基於 accept 請求頭方式(如:Accept: application/json; version=1.0):

 

settings.py: REST_FRAMEWORK = { 'DEFAULT_VERSION': 'v1',  # 默認版本
        'ALLOWED_VERSIONS': ['v1', 'v2'],  # 允許的版本
        'VERSION_PARAM': 'version'  # URL中獲取值的key
 } urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'^test/', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.versioning import AcceptHeaderVersioning class TestView(APIView): versioning_class = AcceptHeaderVersioning def get(self, request, *args, **kwargs): # 獲取版本 HTTP_ACCEPT頭
            print(request.version) # 獲取版本管理的類
            print(request.versioning_scheme) # 反向生成URL
            reverse_url = request.versioning_scheme.reverse('test', request=request) print(reverse_url) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

4.3.4 基於主機名方法(如:v1.example.com):

settings.py: ALLOWED_HOSTS = ['*'] REST_FRAMEWORK = { 'DEFAULT_VERSION': 'v1',  # 默認版本
        'ALLOWED_VERSIONS': ['v1', 'v2'],  # 允許的版本
        'VERSION_PARAM': 'version'  # URL中獲取值的key
 } urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'^test/', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.versioning import HostNameVersioning class TestView(APIView): versioning_class = HostNameVersioning def get(self, request, *args, **kwargs): # 獲取版本
            print(request.version) # 獲取版本管理的類
            print(request.versioning_scheme) # 反向生成URL
            reverse_url = request.versioning_scheme.reverse('test', request=request) print(reverse_url) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

 

4.3.5  基於django路由系統的namespace(如:example.com/v1/users/):

settings.py: REST_FRAMEWORK = { 'DEFAULT_VERSION': 'v1',  # 默認版本
        'ALLOWED_VERSIONS': ['v1', 'v2'],  # 允許的版本
        'VERSION_PARAM': 'version'  # URL中獲取值的key
 } urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'^v1/', ([url(r'test/', TestView.as_view(), name='test'), ], None, 'v1')), url(r'^v2/', ([url(r'test/', TestView.as_view(), name='test'), ], None, 'v2')), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.versioning import NamespaceVersioning class TestView(APIView): versioning_class = NamespaceVersioning def get(self, request, *args, **kwargs): # 獲取版本
            print(request.version) # 獲取版本管理的類
            print(request.versioning_scheme) # 反向生成URL
            reverse_url = request.versioning_scheme.reverse('test', request=request) print(reverse_url) return Response('GET請求,響應內容') def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

 

4.3.6 全局使用:

settings.py: REST_FRAMEWORK = { 'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.URLPathVersioning", 'DEFAULT_VERSION': 'v1', 'ALLOWED_VERSIONS': ['v1', 'v2'], 'VERSION_PARAM': 'version' }

 

4.4 解析器(parser):(根據請求頭 content-type 選擇對應的解析器就請求體內容進行處理。)

4.4.1 僅處理請求頭content-type為application/json的請求體:

 

urls.py: from django.conf.urls import url, include from web.views.s5_parser import TestView urlpatterns = [ url(r'test/', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.request import Request from rest_framework.parsers import JSONParser class TestView(APIView): parser_classes = [JSONParser, ] def post(self, request, *args, **kwargs): print(request.content_type) # 獲取請求的值,並使用對應的JSONParser進行處理
            print(request.data) # application/x-www-form-urlencoded 或 multipart/form-data時, request.POST中才有值
            print(request.POST) print(request.FILES) return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

 

 

 

4.4.2 僅處理請求頭content-type為application/x-www-form-urlencoded 的請求體:

urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'test/', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.request import Request from rest_framework.parsers import FormParser class TestView(APIView): parser_classes = [FormParser, ] def post(self, request, *args, **kwargs): print(request.content_type) # 獲取請求的值,並使用對應的JSONParser進行處理
            print(request.data) # application/x-www-form-urlencoded 或 multipart/form-data時, request.POST中才有值
            print(request.POST) print(request.FILES) return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

 

4.4.3 僅處理請求頭content-type為multipart/form-data的請求體

urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'test/', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.request import Request from rest_framework.parsers import MultiPartParser class TestView(APIView): parser_classes = [MultiPartParser, ] def post(self, request, *args, **kwargs): print(request.content_type) # 獲取請求的值,並使用對應的JSONParser進行處理
            print(request.data) # application/x-www-form-urlencoded 或 multipart/form-data時, request.POST中才有值
            print(request.POST) print(request.FILES) return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容') upload.html: <!DOCTYPE html>
    <html lang = "en">
    <head>
        <meta charset = "UTF-8">
        <title> Title </title>
    </head>
    <body>
    <form action = "http://127.0.0.1:8000/test/" method = "post" enctype = "multipart/form-data">
      <input type = "text" name = "user"/>
      <input type = "file" name = "img">
      <input type = "submit" value = "提交">
    </form>
    </body>
    </html>

 

4.4.4 僅上傳文件:

urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'test/(?P<filename>[^/]+)', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.request import Request from rest_framework.parsers import FileUploadParser class TestView(APIView): parser_classes = [FileUploadParser, ] def post(self, request, filename, *args, **kwargs): print(filename) print(request.content_type) # 獲取請求的值,並使用對應的JSONParser進行處理
            print(request.data) # application/x-www-form-urlencoded 或 multipart/form-data時, request.POST中才有值
            print(request.POST) print(request.FILES) return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容') upload.html: <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="http://127.0.0.1:8000/test/f1.numbers" method="post" enctype="multipart/form-data">
        <input type="text" name="user" />
        <input type="file" name="img">
        <input type="submit" value="提交">
    </form>
    </body>
    </html>

 

4.4.5 同時多個Parser(當同時使用多個parser時,rest framework會根據請求頭content-type自動進行比對,並使用對應parser)

urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'test/', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.request import Request from rest_framework.parsers import JSONParser, FormParser, MultiPartParser class TestView(APIView): parser_classes = [JSONParser, FormParser, MultiPartParser, ] def post(self, request, *args, **kwargs): print(request.content_type) # 獲取請求的值,並使用對應的JSONParser進行處理
            print(request.data) # application/x-www-form-urlencoded 或 multipart/form-data時, request.POST中才有值
            print(request.POST) print(request.FILES) return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

4.4.6 全局使用:

settings.py: REST_FRAMEWORK = { 'DEFAULT_PARSER_CLASSES':[ 'rest_framework.parsers.JSONParser'
            'rest_framework.parsers.FormParser'
            'rest_framework.parsers.MultiPartParser' ] } urls.py: from django.conf.urls import url, include from web.views import TestView urlpatterns = [ url(r'test/', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response class TestView(APIView): def post(self, request, *args, **kwargs): print(request.content_type) # 獲取請求的值,並使用對應的JSONParser進行處理
            print(request.data) # application/x-www-form-urlencoded 或 multipart/form-data時, request.POST中才有值
            print(request.POST) print(request.FILES) return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')

 

4.5 序列化(序列化用於對用戶請求數據進行驗證和數據進行序列化。)

4.5.1 自定義字段:

urls.py: from django.conf.urls import url, include from web.views.s6_serializers import TestView urlpatterns = [ url(r'test/', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import serializers from .. import models class PasswordValidator(object): def __init__(self, base): self.base = base def __call__(self, value): if value != self.base: message = 'This field must be %s.' % self.base raise serializers.ValidationError(message) def set_context(self, serializer_field): """ This hook is called by the serializer instance, prior to the validation call being made. """
            # 執行驗證之前調用,serializer_fields是當前字段對象
            pass
    
    
    class UserSerializer(serializers.Serializer): ut_title = serializers.CharField(source='ut.title') user = serializers.CharField(min_length=6) pwd = serializers.CharField(error_messages={'required': '密碼不能為空'}, validators=[PasswordValidator('666')]) class TestView(APIView): def get(self, request, *args, **kwargs): # 序列化,將數據庫查詢字段序列化為字典
            data_list = models.UserInfo.objects.all() ser = UserSerializer(instance=data_list, many=True) #
            # obj = models.UserInfo.objects.all().first()
            # ser = UserSerializer(instance=obj, many=False)
            return Response(ser.data) def post(self, request, *args, **kwargs): # 驗證,對請求發來的數據進行驗證
            ser = UserSerializer(data=request.data) if ser.is_valid(): print(ser.validated_data) else: print(ser.errors) return Response('POST請求,響應內容')

 

4.5.2 基於Model自動生成字段:

urls.py: from django.conf.urls import url, include from web.views.s6_serializers import TestView urlpatterns = [ url(r'test/', TestView.as_view(), name='test'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import serializers from .. import models class PasswordValidator(object): def __init__(self, base): self.base = str(base) def __call__(self, value): if value != self.base: message = 'This field must be %s.' % self.base raise serializers.ValidationError(message) def set_context(self, serializer_field): """ This hook is called by the serializer instance, prior to the validation call being made. """
            # 執行驗證之前調用,serializer_fields是當前字段對象
            pass

    class ModelUserSerializer(serializers.ModelSerializer): user = serializers.CharField(max_length=32) class Meta: model = models.UserInfo fields = "__all__"
            # fields = ['user', 'pwd', 'ut']
            depth = 2 extra_kwargs = {'user': {'min_length': 6}, 'pwd': {'validators':                                     [PasswordValidator(666), ]}} # read_only_fields = ['user']

    class TestView(APIView): def get(self, request, *args, **kwargs): # 序列化,將數據庫查詢字段序列化為字典
            data_list = models.UserInfo.objects.all() ser = ModelUserSerializer(instance=data_list, many=True) #
            # obj = models.UserInfo.objects.all().first()
            # ser = UserSerializer(instance=obj, many=False)
            return Response(ser.data) def post(self, request, *args, **kwargs): # 驗證,對請求發來的數據進行驗證
            print(request.data) ser = ModelUserSerializer(data=request.data) if ser.is_valid(): print(ser.validated_data) else: print(ser.errors) return Response('POST請求,響應內容')

 

4.5.3 生成URL:

urls.py: from django.conf.urls import url, include from web.views.s6_serializers import TestView urlpatterns = [ url(r'test/', TestView.as_view(), name='test'), url(r'detail/(?P<pk>\d+)/', TestView.as_view(), name='detail'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import serializers from .. import models class PasswordValidator(object): def __init__(self, base): self.base = str(base) def __call__(self, value): if value != self.base: message = 'This field must be %s.' % self.base raise serializers.ValidationError(message) def set_context(self, serializer_field): """ This hook is called by the serializer instance, prior to the validation call being made. """
            # 執行驗證之前調用,serializer_fields是當前字段對象
            pass

    class ModelUserSerializer(serializers.ModelSerializer): ut = serializers.HyperlinkedIdentityField(view_name='detail') class Meta: model = models.UserInfo fields = "__all__" extra_kwargs = { 'user': {'min_length': 6}, 'pwd': {'validators': [PasswordValidator(666),]}, } class TestView(APIView): def get(self, request, *args, **kwargs): # 序列化,將數據庫查詢字段序列化為字典
            data_list = models.UserInfo.objects.all() ser = ModelUserSerializer(instance=data_list, many=True, context={'request': request}) #
            # obj = models.UserInfo.objects.all().first()
            # ser = UserSerializer(instance=obj, many=False)
            return Response(ser.data) def post(self, request, *args, **kwargs): # 驗證,對請求發來的數據進行驗證
            print(request.data) ser = ModelUserSerializer(data=request.data) if ser.is_valid(): print(ser.validated_data) else: print(ser.errors) return Response('POST請求,響應內容')

4.5.4 自動生成URL:

urls.py: from django.conf.urls import url, include from web.views.s6_serializers import TestView urlpatterns = [ url(r'test/', TestView.as_view(), name='test'), url(r'detail/(?P<pk>\d+)/', TestView.as_view(), name='xxxx'), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import serializers from .. import models class PasswordValidator(object): def __init__(self, base): self.base = str(base) def __call__(self, value): if value != self.base: message = 'This field must be %s.' % self.base raise serializers.ValidationError(message) def set_context(self, serializer_field): """ This hook is called by the serializer instance, prior to the validation call being made. """
            # 執行驗證之前調用,serializer_fields是當前字段對象
            pass

    class ModelUserSerializer(serializers.HyperlinkedModelSerializer): ll = serializers.HyperlinkedIdentityField(view_name='xxxx') tt = serializers.CharField(required=False) class Meta: model = models.UserInfo fields = "__all__" list_serializer_class = serializers.ListSerializer extra_kwargs = { 'user': {'min_length': 6}, 'pwd': {'validators': [PasswordValidator(666), ]}, 'url': {'view_name': 'xxxx'}, 'ut': {'view_name': 'xxxx'}, } class TestView(APIView): def get(self, request, *args, **kwargs): # # 序列化,將數據庫查詢字段序列化為字典
            data_list = models.UserInfo.objects.all() ser = ModelUserSerializer(instance=data_list, many=True, context={'request': request}) # # 如果Many=True
            # # 或
            # # obj = models.UserInfo.objects.all().first()
            # # ser = UserSerializer(instance=obj, many=False)
            return Response(ser.data) def post(self, request, *args, **kwargs): # 驗證,對請求發來的數據進行驗證
            print(request.data) ser = ModelUserSerializer(data=request.data) if ser.is_valid(): print(ser.validated_data) else: print(ser.errors) return Response('POST請求,響應內容')

 

4.6 分頁:

4.6.1 根據頁碼進行分頁:

urls.py: from django.conf.urls import url, include from rest_framework import routers from web.views import s9_pagination urlpatterns = [ url(r'^test/', s9_pagination.UserViewSet.as_view()), ] views.py: from rest_framework.views import APIView from rest_framework import serializers from .. import models from rest_framework.pagination import PageNumberPagination class StandardResultsSetPagination(PageNumberPagination): # 默認每頁顯示的數據條數
        page_size = 1
        # 獲取URL參數中設置的每頁顯示數據條數
        page_size_query_param = 'page_size'
        # 獲取URL參數中傳入的頁碼key
        page_query_param = 'page'
        # 最大支持的每頁顯示的數據條數
        max_page_size = 1

    class UserSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo fields = "__all__"

    class UserViewSet(APIView): def get(self, request, *args, **kwargs): user_list = models.UserInfo.objects.all().order_by('-id') # 實例化分頁對象,獲取數據庫中的分頁數據
            paginator = StandardResultsSetPagination() page_user_list = paginator.paginate_queryset(user_list, self.request, view=self) # 序列化對象
            serializer = UserSerializer(page_user_list, many=True) # 生成分頁和數據
            response = paginator.get_paginated_response(serializer.data) return response

 

4.6.2 位置和個數進行分頁:

urls.py: from django.conf.urls import url, include from web.views import s9_pagination urlpatterns = [ url(r'^test/', s9_pagination.UserViewSet.as_view()), ] views.py: from rest_framework.views import APIView from rest_framework import serializers from .. import models from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination class StandardResultsSetPagination(LimitOffsetPagination): # 默認每頁顯示的數據條數
        default_limit = 10
        # URL中傳入的顯示數據條數的參數
        limit_query_param = 'limit'
        # URL中傳入的數據位置的參數
        offset_query_param = 'offset'
        # 最大每頁顯得條數
        max_limit = None class UserSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo fields = "__all__"

    class UserViewSet(APIView): def get(self, request, *args, **kwargs): user_list = models.UserInfo.objects.all().order_by('-id') # 實例化分頁對象,獲取數據庫中的分頁數據
            paginator = StandardResultsSetPagination() page_user_list = paginator.paginate_queryset(user_list, self.request, view=self) # 序列化對象
            serializer = UserSerializer(page_user_list, many=True) # 生成分頁和數據
            response = paginator.get_paginated_response(serializer.data) return response

 

4.6.3 游標分頁:

urls.py: from django.conf.urls import url, include from web.views import s9_pagination urlpatterns = [ url(r'^test/', s9_pagination.UserViewSet.as_view()), ] views.py: from rest_framework.views import APIView from rest_framework import serializers from .. import models from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination class StandardResultsSetPagination(CursorPagination): # URL傳入的游標參數
        cursor_query_param = 'cursor'
        # 默認每頁顯示的數據條數
        page_size = 2
        # URL傳入的每頁顯示條數的參數
        page_size_query_param = 'page_size'
        # 每頁顯示數據最大條數
        max_page_size = 1000
        # 根據ID從大到小排列
        ordering = "id"
    
    
    class UserSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo fields = "__all__"
    
    
    class UserViewSet(APIView): def get(self, request, *args, **kwargs): user_list = models.UserInfo.objects.all().order_by('-id') # 實例化分頁對象,獲取數據庫中的分頁數據
            paginator = StandardResultsSetPagination() page_user_list = paginator.paginate_queryset(user_list, self.request, view=self) # 序列化對象
            serializer = UserSerializer(page_user_list, many=True) # 生成分頁和數據
            response = paginator.get_paginated_response(serializer.data) return response

 

4.7 路由系統:

4.7.1 自定義路由:

urls.py: from django.conf.urls import url, include from web.views import s11_render urlpatterns = [ url(r'^test/$', s11_render.TestView.as_view()), url(r'^test\.(?P<format>[a-z0-9]+)$', s11_render.TestView.as_view()), url(r'^test/(?P<pk>[^/.]+)/$', s11_render.TestView.as_view()), url(r'^test/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)$', s11_render.TestView.as_view()) ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from .. import models class TestView(APIView): def get(self, request, *args, **kwargs): print(kwargs) print(self.renderer_classes) return Response('...')

4.7.2 半自動路由:

urls.py: from django.conf.urls import url, include from web.views import s10_generic urlpatterns = [ url(r'^test/$', s10_generic.UserViewSet.as_view({'get': 'list', 'post': 'create'})), url(r'^test/(?P<pk>\d+)/$', s10_generic.UserViewSet.as_view( {'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'})), ] views.py: from rest_framework.viewsets import ModelViewSet from rest_framework import serializers from .. import models class UserSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo fields = "__all__"

    class UserViewSet(ModelViewSet): queryset = models.UserInfo.objects.all() serializer_class = UserSerializer

4.7.3 全自動路由:

urls.py: from django.conf.urls import url, include from rest_framework import routers from web.views import s10_generic router = routers.DefaultRouter() router.register(r'users', s10_generic.UserViewSet) urlpatterns = [ url(r'^', include(router.urls)), ] views.py: from rest_framework.viewsets import ModelViewSet from rest_framework import serializers from .. import models class UserSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo fields = "__all__"
    
    
    class UserViewSet(ModelViewSet): queryset = models.UserInfo.objects.all() serializer_class = UserSerializer

 

4.8 視圖:

4.8.1 GenericViewSet:

urls.py: from django.conf.urls import url, include from web.views.s7_viewset import TestView urlpatterns = [ url(r'test/', TestView.as_view({'get':'list'}), name='test'), url(r'detail/(?P<pk>\d+)/', TestView.as_view({'get':'list'}), name='xxxx'), ] views.py: from rest_framework import viewsets from rest_framework.response import Response class TestView(viewsets.GenericViewSet): def list(self, request, *args, **kwargs): return Response('...') def add(self, request, *args, **kwargs): pass
        def delete(self, request, *args, **kwargs): pass
        def edit(self, request, *args, **kwargs): pass

4.8.2 ModelViewSet(自定義URL):

urls.py: from django.conf.urls import url, include from web.views import s10_generic urlpatterns = [ url(r'^test/$', s10_generic.UserViewSet.as_view({'get': 'list', 'post': 'create'})), url(r'^test/(?P<pk>\d+)/$', s10_generic.UserViewSet.as_view( {'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'})), ] views.py: from rest_framework.viewsets import ModelViewSet from rest_framework import serializers from .. import models class UserSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo fields = "__all__"

    class UserViewSet(ModelViewSet): queryset = models.UserInfo.objects.all() serializer_class = UserSerializer

 

4.8.3 ModelViewSet(rest framework路由):

urls.py: from django.conf.urls import url, include from rest_framework import routers from app01 import views router = routers.DefaultRouter() router.register(r'users', views.UserViewSet) router.register(r'groups', views.GroupViewSet) # Wire up our API using automatic URL routing.
    # Additionally, we include login URLs for the browsable API.
    urlpatterns = [ url(r'^', include(router.urls)), ] views.py: from rest_framework import viewsets from rest_framework import serializers class UserSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = models.User fields = ('url', 'username', 'email', 'groups') class GroupSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = models.Group fields = ('url', 'name') class UserViewSet(viewsets.ModelViewSet): """ API endpoint that allows users to be viewed or edited. """ queryset = User.objects.all().order_by('-date_joined') serializer_class = UserSerializer class GroupViewSet(viewsets.ModelViewSet): """ API endpoint that allows groups to be viewed or edited. """ queryset = Group.objects.all() serializer_class = GroupSerializer

4.9 渲染器:

4.9.1 簡介:

  根據 用戶請求URL 或 用戶可接受的類型,篩選出合適的 渲染組件。

  用戶請求URL:

    http://127.0.0.1:8000/test/?format=json

    http://127.0.0.1:8000/test.json

  用戶請求頭:

    Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8

4.9.2 json: 

  訪問URL:

 

    http://127.0.0.1:8000/test/?format=json

 

    http://127.0.0.1:8000/test.json

 

    http://127.0.0.1:8000/test/ 

urls.py: from django.conf.urls import url, include from web.views import s11_render urlpatterns = [ url(r'^test/$', s11_render.TestView.as_view()), url(r'^test\.(?P<format>[a-z0-9]+)', s11_render.TestView.as_view()), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import serializers from rest_framework.renderers import JSONRenderer from .. import models class TestSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo fields = "__all__"
    
    
    class TestView(APIView): renderer_classes = [JSONRenderer, ] def get(self, request, *args, **kwargs): user_list = models.UserInfo.objects.all() ser = TestSerializer(instance=user_list, many=True) return Response(ser.data)

 

4.9.3 表格:

  訪問URL:

    http://127.0.0.1:8000/test/?format=admin

    http://127.0.0.1:8000/test.admin

    http://127.0.0.1:8000/test/ 

views.py: #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import serializers from rest_framework.renderers import AdminRenderer from .. import models class TestSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo fields = "__all__"

    class TestView(APIView): renderer_classes = [AdminRenderer, ] def get(self, request, *args, **kwargs): user_list = models.UserInfo.objects.all() ser = TestSerializer(instance=user_list, many=True) return Response(ser.data)

 

4.9.4 Form表單: 

  訪問URL:

    http://127.0.0.1:8000/test/?format=form

    http://127.0.0.1:8000/test.form

    http://127.0.0.1:8000/test/ 

views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import serializers from rest_framework.renderers import JSONRenderer from rest_framework.renderers import AdminRenderer from rest_framework.renderers import HTMLFormRenderer from .. import models class TestSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo fields = "__all__"

    class TestView(APIView): renderer_classes = [HTMLFormRenderer, ] def get(self, request, *args, **kwargs): user_list = models.UserInfo.objects.all().first() ser = TestSerializer(instance=user_list, many=False) return Response(ser.data)

 

4.9.5 自定義顯示模板:

  訪問URL:

    http://127.0.0.1:8000/test/?format=html

    http://127.0.0.1:8000/test.html

    http://127.0.0.1:8000/test/

urls.py: from django.conf.urls import url, include from web.views import s11_render urlpatterns = [ url(r'^test/$', s11_render.TestView.as_view()), url(r'^test\.(?P<format>[a-z0-9]+)', s11_render.TestView.as_view()), ] views.py: from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import serializers from rest_framework.renderers import TemplateHTMLRenderer from .. import models class TestSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo fields = "__all__"
    
    
    class TestView(APIView): renderer_classes = [TemplateHTMLRenderer, ] def get(self, request, *args, **kwargs): user_list = models.UserInfo.objects.all().first() ser = TestSerializer(instance=user_list, many=False) return Response(ser.data, template_name='user_detail.html') userdetail.html: <!DOCTYPE html>
    <html lang = "en">
    <head>
        <meta charset = "UTF-8">
        <title> Title </title>
    </head>
    <body> {{user}} {{pwd}} {{ut}} </body>
    </html>

4.9.5 瀏覽器格式API+JSON:

  訪問URL:

    http://127.0.0.1:8000/test/?format=api

    http://127.0.0.1:8000/test.api

    http://127.0.0.1:8000/test/ 

views.py: #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import serializers from rest_framework.renderers import JSONRenderer from rest_framework.renderers import BrowsableAPIRenderer from .. import models class TestSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo fields = "__all__"

    class CustomBrowsableAPIRenderer(BrowsableAPIRenderer): def get_default_renderer(self, view): return JSONRenderer() class TestView(APIView): renderer_classes = [CustomBrowsableAPIRenderer, ] def get(self, request, *args, **kwargs): user_list = models.UserInfo.objects.all().first() ser = TestSerializer(instance=user_list, many=False) return Response(ser.data, template_name='user_detail.html')

 

注意:如果同時多個存在時,自動根據URL后綴來選擇渲染器。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM