DRF环境配置
- 导入需要的第三方库
pip install djangoframework
pip install markdown
pip install django-filter
- 在settings.py中激活 rest_framework 框架
INSTALLED_APPS = [
...
'rest_framework'
]
Django中的请求和响应
-
request (WSGIRequest)
-
request.GET , request.POST , request.FILES
-
request.method
-
request.content_type
-
HttResponse, render, redirect , JsonResponse
DRF中的请求和响应
- request
- reuqest.data (只能接收 POST,PUT, DELETE , FILES 提交的数据)
- request.query_params (用来获取 GET 提交的数据)
- request.method
- request.content_type
- 把一个 django中 request 对象,转换成 drf 中的 request ,
需要提交一个 api_view 装饰器
from rest_framework.decorators import api_view
@api_view(["GET"])
def ...
- Response(data, status, content_type)
- data : 响应的 json数据
- status : 响应的状态码,默认是200
- content_type : 响应的类型,默认是 application/json;charset=UTF-8