Django-djangorestframework-响应模块

响应模块

一般都用 Response 对象来做返回(最后一定是打包成符合 HTTP 协议的数据格式来传输,Response 类做了一系列处理,所以这里我们只需要关注下它的那些参数即可)

响应类构造器 rest_framework.response.Response

def __init__(self, data=None, status=None,
                 template_name=None, headers=None,
                 exception=False, content_type=None):
     """
        :param data: 响应数据
        :param status: http响应状态码
        :param template_name: drf也可以渲染页面,渲染的页面模板地址(不用了解)
        :param headers: 响应头
        :param exception: 是否异常了
        :param content_type: 响应的数据格式(一般不用处理,响应头中带了,且默认是json)
    """
    pass

常规实例化响应对象

from rest_framework.response import Response
from rest_framework import status
# status就是解释一堆 数字 网络状态码的模块

# 一般情况下只需要返回数据,status和headers都有默认值
return Response(data={数据}, status=status.HTTP_200_OK, headers={设置的响应头})
原文地址:https://www.cnblogs.com/suwanbin/p/12013604.html