REST framework 基本使用

安裝

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) 

參考鏈接於此

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章