一. 响应器
https://blog.csdn.net/qq_33961117/article/details/85055376 响应器(渲染器)
https://www.cnblogs.com/zhigu/p/10145317.html
1. 响应器显示
1-1 默认显示 - format=api 1-2 JSON格式显示 format=json 1-3 form表单方式:HTMLFormRenderer - ?format=form 1-4 表格方式:AdminRenderer - ?format=admin 二、响应器的配置 2-1 局部使用 2-2 全局使用 - settings配置
一、响应器显示
1-1 默认显示 - format=api
1-2 JSON格式显示 format=json
1-3 form表单方式:HTMLFormRenderer - ?format=form 1-4 表格方式:AdminRenderer - ?format=admin
二、响应器的配置
2-1 局部使用 from rest_framework.renderers import HTMLFormRenderer,BrowsableAPIRenderer class BookDetailView(APIView): renderer_classes = [HTMLFormRenderer,BrowsableAPIRenderer ] def get(self,request,pk): book_obj=models.Book.objects.filter(pk=pk).first() bs=BookSerializers(book_obj,many=False) return Response(bs.data) def put(self,request,pk): book_obj = models.Book.objects.filter(pk=pk).first() bs=BookSerializers(data=request.data,instance=book_obj) if bs.is_valid(): bs.save() # update return Response(bs.data) else: return Response(bs.errors) def delete(self,request,pk): models.Book.objects.filter(pk=pk).delete() return Response("")
2-2 全局使用 - settings配置
REST_FRAMEWORK = { 'DEFAULT_RENDERER_CLASSES':['rest_framework.renderers.JSONRenderer']
三、自定义显示默认页面
from rest_framework.renderers import TemplateHTMLRenderer class BookDetailView(APIView): renderer_classes = [TemplateHTMLRenderer] def get(self,request,pk): book_obj=models.Book.objects.filter(pk=pk).first() bs=BookSerializers(book_obj,many=False) return Response(bs.data,template_name='aa.html')