安裝
pip install djangorestframework
djangorestframework 介紹
-
djangorestframework 主要使用 APIView,其實APIView實質是對 View 進行繼承加工了更多功能
-
請求來了 APIView首先執行 self.dispatch 方法,此方法對 request 進行了再次封裝
基於django實現REST framework
urls.py
urlpatterns = [
url(r'^users', Users.as_view()),
]
views,py
from django.views import View
from django.http import JsonResponse
class Users(View):
def get(self, request, *args, **kwargs):
result = {
'status': True,
'data': 'response data'
}
return JsonResponse(result, status=200)
def post(self, request, *args, **kwargs):
result = {
'status': True,
'data': 'response data'
}
return JsonResponse(result, status=200)