标签:get import str rom 全局 请求 error response pre
https://www.cnblogs.com/liuqingzheng/articles/9766413.html
根据 用户请求URL 或 用户可接受的类型,筛选出合适的 渲染组件。
用户请求URL:
http://127.0.0.1:8000/test/?format=json
http://127.0.0.1:8000/test.json
显示json格式:JSONRenderer
访问URL:
默认显示格式:BrowsableAPIRenderer(可以修改它的html文件)
访问URL:
表格方式:AdminRenderer
访问URL:
form表单方式:HTMLFormRenderer
访问URL:
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("")
settings里配置:
REST_FRAMEWORK = { ‘DEFAULT_RENDERER_CLASSES‘:[‘rest_framework.renderers.JSONRenderer‘] }
标签:get import str rom 全局 请求 error response pre
原文地址:https://www.cnblogs.com/cao123/p/10153833.html