1. 程式人生 > 實用技巧 >03-四大基本模組

03-四大基本模組

DRF框架介紹

安裝

pip install djangorestframework

DRF框架規範的封裝風格

# 檢視: from rest_framework.views import APIView
# 響應: from rest_framework.response import Responses
# 請求: from rest_framework.request import Request
# 序列化: from rest_framework.serializers import Serializer
# 配置: from rest_framework.settings import APISettings
# 過濾器: from rest_framework.filters import SearchFilter # 分頁: from rest_framework.pagination import PageNumberPagination # 認證: from rest_framework.authentication import TokenAuthentication # 認證: from rest_framework.permissions import IsAuthenticated # 認證: from rest_framework.throttling import SimpleRateThrottle
from rest_framework.views import APIView class Test(APIView): def get(self, request, *args, **kwargs): return Response('drf get ok')

DRF請求生命週期

1) 請求走的是APIView的as_view函式
2) 在APIView的as_view呼叫父類(django原生)的as_view, 還禁用了csrf認證
3) 在父類的as_view中dispatch方法請求走的又是APIView的dispatch
4) 完成任務方法交給檢視類的請求函式處理, 得到請求的響應結果, 返回給前臺

APIView(as_view())禁用csrf => View(as_view()) => APIView(dispatch()) => 檢視類的請求方法 => 響應