码迷,mamicode.com
首页 > 其他好文 > 详细

rest framework 之渲染器

时间:2019-08-19 22:41:04      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:用户   rom   render   参考   template   html   表格   detail   roles   

根据 用户请求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 之渲染器

标签:用户   rom   render   参考   template   html   表格   detail   roles   

原文地址:https://www.cnblogs.com/midworld/p/11380195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!