「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',]
}
View Code

局部配置:

class PagesView(ModelViewSet):
    renderer_classes = [JSONRenderer,BrowsableAPIRenderer,AdminRenderer]
    queryset = models.UserInfo.objects.all()
    serializer_class = pages.PagesSerializer
    pagination_class = MyPageNumberPagination
View Code

补充:
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
View Code

 



posted @ 2019-02-19 17:42  子谦0618  阅读(100)  评论(0编辑  收藏  举报