安装
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)