「Django」rest_framework学习系列-渲染器
渲染器:作用于页面,JSONRenderer只是JSON格式,BrowsableAPIRenderer有页面,.AdminRenderer页面以admin形式呈现(需要在请求地址后缀添加?fromat=admin)
渲染器全局与局部配置方法
全局配置:
REST_FRAMEWORK = { #渲染器配置 'DEFAULT_RENDERER_CLASSES': ['rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.BrowsableAPIRenderer', 'rest_framework.renderers.AdminRenderer',] }
局部配置:
class PagesView(ModelViewSet): renderer_classes = [JSONRenderer,BrowsableAPIRenderer,AdminRenderer] queryset = models.UserInfo.objects.all() serializer_class = pages.PagesSerializer pagination_class = MyPageNumberPagination
补充:
BrowsableAPIRenderer是界面,也支持重写,可以重写或修改页面
源码为:
class BrowsableAPIRenderer(BaseRenderer): media_type = 'text/html' format = 'api' template = 'rest_framework/api.html' filter_template = 'rest_framework/filters/base.html' code_style = 'emacs' charset = 'utf-8' form_renderer_class = HTMLFormRenderer