zoukankan      html  css  js  c++  java
  • rest framework 之路由系统

    一、自定义路由

    1、urls.py

    from django.conf.urls import url, include
    from web.views import s11_render
    
    urlpatterns = [
        url(r'^test/$', s11_render.TestView.as_view()),
        url(r'^test.(?P<format>[a-z0-9]+)$', s11_render.TestView.as_view()),
        url(r'^test/(?P<pk>[^/.]+)/$', s11_render.TestView.as_view()),
        url(r'^test/(?P<pk>[^/.]+).(?P<format>[a-z0-9]+)$', s11_render.TestView.as_view())
    ]
    

    2、views.py

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from .. import models
    
    
    class TestView(APIView):
        def get(self, request, *args, **kwargs):
            print(kwargs)
            print(self.renderer_classes)
            return Response('...')
    

    二、半自动路由

    1、urls.py

    from django.conf.urls import url, include
    from web.views import s10_generic
    
    urlpatterns = [
        url(r'^test/$', s10_generic.UserViewSet.as_view({'get': 'list', 'post': 'create'})),
        url(r'^test/(?P<pk>d+)/$', s10_generic.UserViewSet.as_view(
            {'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'})),
    ]
    

    2、views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.viewsets import ModelViewSet
    from rest_framework import serializers
    from .. import models
    
    
    class UserSerializer(serializers.ModelSerializer):
        class Meta:
            model = models.UserInfo
            fields = "__all__"
    
    
    class UserViewSet(ModelViewSet):
        queryset = models.UserInfo.objects.all()
        serializer_class = UserSerializer
    

    三、全自动路由

    1、urls.py

    from django.conf.urls import url, include
    from rest_framework import routers
    from api.views import TestView, TestView1
    
    
    router = routers.DefaultRouter()
    router.register(r'test1', TestView1)
    
    urlpatterns = [
        url(r'^(?P<version>[v1|v2]+)/', include(router.urls)),
    ]
    

    2、views.py

    from rest_framework.viewsets import ModelViewSet
    from rest_framework import serializers
    from .. import models
    
    
    class UserSerializer(serializers.ModelSerializer):
        class Meta:
            model = models.UserInfo
            fields = "__all__"
    
    class TestView1(ModelViewSet):
        queryset = models.Role.objects.all()
        serializer_class = TestSerializers
        pagination_class = PageNumberPagination
    
  • 相关阅读:
    Duilib 入门教程: 怎么创建一个自定义的窗口
    从屏幕右下角升起的弹窗
    最小化窗口至右下角
    MFC 使程序不在任务栏显示
    创建数据库
    sqlite入门基础(一):sqlite3_open,sqlite3_exec,slite3_close
    VC++、MFC中最好的开源项目
    在MFC中,利用GDI绘制橡皮筋效果-直线,圆,椭圆,矩形
    WCHAR char CString等常用类型互转
    CGAL 计算机几何算法库
  • 原文地址:https://www.cnblogs.com/midworld/p/11380180.html
Copyright © 2011-2022 走看看