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请求成功',
        })

你可能感兴趣的:(Drf之框架安装和基本使用(二))