Drf之框架安裝和基本使用(二)

drf安裝

# drf: djangorestframework => pip3 install djangorestframework


from rest_framework.views import APIView
from rest_framework.request import Request
from rest_framework.response import Response

drf的安裝步驟

# 1)安裝drf:pip3 install djangorestframework
# 2)settings.py註冊app:INSTALLED_APPS = [..., 'rest_framework']
# 3)基於cbv完成滿足RSSTful規範的接口

drf具體的使用

# 路由層
from app import views
urlpatterns = [
    url(r'^teachers/', views.Teachers.as_view()),
]

# 視圖層
from rest_framework.views import APIView
from rest_framework.response import Response
class Teachers(APIView):
    def get(self, request, *args, **kwargs):
        salary = request.GET.get('salary')
        print(salary)
        return Response({
            'status': 2,
            'msg': 'get請求成功',
        })

    # 前臺發送數據的方式:formdate | urlencoded | json
    # drf的request都對其二次封裝解析到request.data中
    def post(self, request, *args, **kwargs):
        salary = request.data.get('salary')
        print(salary)
        return Response({
            'status': 2,
            'msg': 'post請求成功',
        })
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章