DRF - 渲染

urls.py:

from django.urls import path, re_path
from drf import views

urlpatterns = [
    path('render/', views.RenderView.as_view()),
]

views.py:

from rest_framework import serializers
from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination
from drf import models
from rest_framework.response import Response


class PagerSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = "__all__"


class RenderView(APIView):
    def get(self, request, *args, **kwargs):
        # 获取 Role 表中的所有数据
        role_obj = models.Role.objects.all()
        # 创建分页对象
        page_obj = PageNumberPagination()
        # 将从数据库中获取的数据进行分页
        role_page = page_obj.paginate_queryset(
            queryset=role_obj,
            request=request,
            view=self,
        )
        # 对数据序列化
        ser = PagerSerializer(instance=role_page, many=True)
        # 使用 rest framework 的 Response 进行返回
        return Response(ser.data)

访问 /render/

将渲染修改为 json

from rest_framework import serializers
from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination
from drf import models
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer


class PagerSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = "__all__"


class RenderView(APIView):

    # 修改渲染为 json
    renderer_classes = [JSONRenderer, ]
    
    def get(self, request, *args, **kwargs):
        # 获取 Role 表中的所有数据
        role_obj = models.Role.objects.all()
        # 创建分页对象
        page_obj = PageNumberPagination()
        # 将从数据库中获取的数据进行分页
        role_page = page_obj.paginate_queryset(
            queryset=role_obj,
            request=request,
            view=self,
        )
        # 对数据序列化
        ser = PagerSerializer(instance=role_page, many=True)
        # 使用 rest framework 的 Response 进行返回
        return Response(ser.data)

访问 /render/

如果想要原先的效果,则设置为 BrowsableAPIRenderer

from rest_framework import serializers
from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination
from drf import models
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer, BrowsableAPIRenderer


class PagerSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = "__all__"


class RenderView(APIView):
    renderer_classes = [JSONRenderer, BrowsableAPIRenderer, ]
    def get(self, request, *args, **kwargs):
        # 获取 Role 表中的所有数据
        role_obj = models.Role.objects.all()
        # 创建分页对象
        page_obj = PageNumberPagination()
        # 将从数据库中获取的数据进行分页
        role_page = page_obj.paginate_queryset(
            queryset=role_obj,
            request=request,
            view=self,
        )
        # 对数据序列化
        ser = PagerSerializer(instance=role_page, many=True)
        # 使用 rest framework 的 Response 进行返回
        return Response(ser.data)

访问 /render/

还有其它一下渲染选项,可参考源码

另外也可以在 settings.py 中进行设置

posted @ 2021-01-20 22:31  Sch01aR#  阅读(112)  评论(0编辑  收藏  举报