classAPIView(View):# The following policies may be set at either globally, or per-view.
renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
JSONRenderer源码
rest_framework.renderers
classJSONRenderer(BaseRenderer):"""
Renderer which serializes to JSON.
"""
media_type ='application/json'format='json'
charset =None
TemplateHTMLRenderer源码
rest_framework.renderers
classTemplateHTMLRenderer(BaseRenderer):"""
An HTML renderer for use with templates.
The data supplied to the Response object should be a dictionary that will
be used as context for the template.
The template name is determined by (in order of preference):
1. An explicit `.template_name` attribute set on the response.
2. An explicit `.template_name` attribute set on this class.
3. The return result of calling `view.get_template_names()`.
For example:
data = {'users': User.objects.all()}
return Response(data, template_name='users.html')
For pre-rendered HTML, see StaticHTMLRenderer.
"""
media_type ='text/html'format='html'
charset ='utf-8'
StaticHTMLRenderer源码
rest_framework.renderers
classStaticHTMLRenderer(TemplateHTMLRenderer):"""
An HTML renderer class that simply returns pre-rendered HTML.
The data supplied to the Response object should be a string representing
the pre-rendered HTML content.
For example:
data = '<html><body>example</body></html>'
return Response(data)
For template rendered HTML, see TemplateHTMLRenderer.
"""
media_type ='text/html'format='html'
charset ='utf-8'
HTMLFormRenderer源码
rest_framework.renderers
classHTMLFormRenderer(BaseRenderer):"""
Renderers serializer data into an HTML form.
If the serializer was instantiated without an object then this will
return an HTML form not bound to any object,
otherwise it will return an HTML form with the appropriate initial data
populated from the object.
Note that rendering of field and form errors is not currently supported.
"""
media_type ='text/html'format='form'
charset ='utf-8'
BrowsableAPIRenderer源码
rest_framework.renderers
classBrowsableAPIRenderer(BaseRenderer):"""
HTML renderer used to self-document the API.
"""
media_type ='text/html'format='api'
charset ='utf-8'
from django.contrib.auth.models import User
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
classUserCountView(APIView):"""
A view that returns the count of active users in JSON.
"""
renderer_classes =[JSONRenderer]defget(self, request,format=None):
user_count = User.objects.filter(active=True).count()
content ={'user_count': user_count}return Response(content)
基于函数视图的解析器配置示例
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.response import Response
from myproject.models import User
@api_view(['GET'])@renderer_classes([JSONRenderer])defuser_count_view(request,format=None):"""
A view that returns the count of active users in JSON.
"""
user_count = User.objects.filter(active=True).count()
content ={'user_count': user_count}return Response(content)
自定义渲染器示例
data 请求数据,由 Response() 实例化设置。
accepted_media_type 内容协商阶段确定的可接受的媒体类型。
renderer_context 视图提供的上下文信息字典。
from django.utils.encoding import smart_unicode
from rest_framework import renderers
classPlainTextRenderer(renderers.BaseRenderer):
media_type ='text/plain'format='txt'defrender(self, data, media_type=None, renderer_context=None):return data.encode(self.charset)
高级渲染器用法
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.response import Response
from myproject.models import User
from myproject.serializer import UserSerializer
@api_view(('GET',))@renderer_classes((TemplateHTMLRenderer, JSONRenderer))deflist_users(request):"""
可以返回系统中用户的 JSON 或 HTML 表示的视图。
"""
queryset = Users.objects.filter(active=True)if request.accepted_renderer.format=='html':# TemplateHTMLRenderer 采用上下文字典,并且还需要 template_name 名称。# 它不需要序列化。
data ={'users': queryset}return Response(data, template_name='list_users.html')# JSONRenderer 需要正常的序列化数据。
serializer = UserSerializer(instance=queryset)
data = serializer.data
return Response(data)