rest framework 之渲染器

Posted midworld

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rest framework 之渲染器相关的知识,希望对你有一定的参考价值。

根据 用户请求URL 或 用户可接受的类型,筛选出合适的 渲染组件。

用户请求头:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8

一、JSON

1、urls.py

from django.urls import path, re_path, include
from api.views import TestView2


urlpatterns = [
    re_path(r'(?P<version>[v1|v2]+)/test2', TestView2.as_view()),
]

2、views.py

from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer
from rest_framework import serializers


class Test2Serializers(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = '__all__'


class TestView2(APIView):
    renderer_classes = [JSONRenderer]

    def get(self, request, *args, **kwargs):
        roles = models.Role.objects.all()
        roles_ser = Test2Serializers(instance=roles, many=True)

        return Response(roles_ser.data)

3、访问 URL:

http://127.0.0.1:8000/api/v1/test2/?format=json
http://127.0.0.1:8000/api/v1/test2/.json
http://127.0.0.1:8000/api/v1/test2

技术图片

二、表格

1、views.py

from rest_framework.response import Response
from rest_framework.renderers import AdminRenderer
from rest_framework import serializers


class Test2Serializers(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = '__all__'


class TestView2(APIView):
    renderer_classes = [AdminRenderer]

    def get(self, request, *args, **kwargs):
        roles = models.Role.objects.all()
        roles_ser = Test2Serializers(instance=roles, many=True)

        return Response(roles_ser.data)

2、访问 URL:

http://127.0.0.1:8000/api/v1/test2/?format=admin
http://127.0.0.1:8000/api/v1/test2/.admin
http://127.0.0.1:8000/api/v1/test2

技术图片

三、Form 表单

1、views.py

from rest_framework.response import Response
from rest_framework.renderers import HTMLFormRenderer
from rest_framework import serializers


class Test2Serializers(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = '__all__'


class TestView2(APIView):
    renderer_classes = [HTMLFormRenderer]

    def get(self, request, *args, **kwargs):
        roles = models.Role.objects.all().first()       # 一个,不是全部
        roles_ser = Test2Serializers(instance=roles, many=False)

        return Response(roles_ser.data)

2、访问 URL:

http://127.0.0.1:8000/api/v1/test2/?format=html
http://127.0.0.1:8000/api/v1/test2/.html
http://127.0.0.1:8000/api/v1/test2

技术图片

四、自定义显示模板

1、views.py

from rest_framework.response import Response
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework import serializers


class Test2Serializers(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = '__all__'


class TestView2(APIView):
    renderer_classes = [TemplateHTMLRenderer]

    def get(self, request, *args, **kwargs):
        roles = models.Role.objects.all().first()       # 一个,不是全部
        roles_ser = Test2Serializers(instance=roles, many=False)

        return Response(roles_ser.data, template_name='roles_detail.html')

2、访问 URL:

http://127.0.0.1:8000/api/v1/test2/?format=html
http://127.0.0.1:8000/api/v1/test2/.html
http://127.0.0.1:8000/api/v1/test2

3、templates/roles_detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
     123 
</body>
</html>

技术图片

五、浏览器格式 API+JSON

1、views.py

from rest_framework.response import Response
from rest_framework.renderers import BrowsableAPIRenderer, JSONRenderer
from rest_framework import serializers


class Test2Serializers(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = '__all__'


class CustomBrowsableAPIRenderer(BrowsableAPIRenderer):
    def get_default_renderer(self, view):
        return JSONRenderer()


class TestView2(APIView):
    renderer_classes = [CustomBrowsableAPIRenderer]

    def get(self, request, *args, **kwargs):
        roles = models.Role.objects.all()
        roles_ser = Test2Serializers(instance=roles, many=True)

        return Response(roles_ser.data)

2、访问 URL:

http://127.0.0.1:8000/api/v1/test2/?format=api
http://127.0.0.1:8000/api/v1/test2/.api
http://127.0.0.1:8000/api/v1/test2

Tips:如果同时多个存在时,自动根据URL后缀来选择渲染器。

技术图片

六、全局配置

REST_FRAMEWORK = 
    "DEFAULT_RENDERER_CLASSER": [
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ]

参考文章:Django Rest Framework

以上是关于rest framework 之渲染器的主要内容,如果未能解决你的问题,请参考以下文章

Request.GET 在自定义 HTML Django Rest Framework 渲染器模板中不可用

rest framework之解析器

rest framework 之解析器

python-django rest framework框架之解析器

Django REST framework 单元测试

Django REST framework之解析器实例以及源码流程分析