• 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 中进行设置

  • 相关阅读:
    初始nginx(启动运行) 使用nginx做一个简单的静态资源服务器
    redis总结问题
    reids高可用(灾难备份-持久化)
    mybatis中将0识别为null的解决方法
    Spring MVC 通过反射将数据导出到excel
    spring mvc 中Uploadify插件的使用
    java搭建 SpringMVC+Mybatis(SMM)+mybatis-generate
    让ecshop用户名、手机号、email登陆方法
    ecmobile实现支付宝支付和百度云推送遇到的问题及解决方案(android)
    Oracle分页总汇
  • 原文地址:https://www.cnblogs.com/sch01ar/p/14305523.html
Copyright © 2020-2023  润新知