Response的使用

构造方式

Response(data, status=None, template_name=None, headers=None, content_type=None)
  • data: 为响应准备的序列化处理后的数据;
  • status: 状态码,默认200;
  • template_name: 模板名称,如果使用HTMLRenderer 时需指明;
  • headers: 用于存放响应头信息的字典;
  • content_type: 响应数据的Content-Type,通常此参数无需传递,REST framework会根据前端所需类型数据来设置该参数。

使用

from . import views
from django.urls import path



urlpatterns=[
    path("booktypes/",views.BookTypes.as_view()),

]
from django.shortcuts import render
from rest_framework.views import APIView
from  rest_framework.response import Response
from django.core.paginator import Paginator
from .models import BookType,Book
from .serializers import  BookTypeSerializer
# Create your views here.



class BookTypes(APIView):

    def get(self,request):
        res_data = {
                "code": 500,
                "msg": "fail",

            }
        return Response(res_data,status=200)

results matching ""

    No results matching ""