python-django rest framework框架之路由

Posted Cool

tags:

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

路由
    
    第一类:原始继承APIView
        # http://127.0.0.1:8000/api/v1/auth/
        url(r^auth/$, views.AuthView.as_view()),

        # http://127.0.0.1:8000/api/v1/auth.json # 想要让页面显示json格式
        url(r^auth\.(?P<format>[a-z0-9]+)$, views.AuthView.as_view()),

        # http://127.0.0.1:8000/api/v1/auth/1/
        url(r^auth/(?P<pk>\d+)/$, views.AuthView.as_view()),

        # http://127.0.0.1:8000/api/v1/auth/1.json
        url(r^auth/(?P<pk>\d+)\.(?P<format>[a-z0-9]+)$, views.AuthView.as_view()),


        class AuthView(views.APIView):

            def get(self,request,*args,**kwargs):
                return Response(...)
    
    第二类: 继承ModelViewSet
        url(r^index/$, views.IndexView.as_view({get:list,post:create})),
        url(r^index/\.(?P<format>[a-z0-9]+)$, views.IndexView.as_view({get:list,post:create})),
        url(r^index/(?P<pk>\d+)/$, views.IndexView.as_view({get:retrieve,delete:destroy,put:update,patch:partial_update})),
        url(r^index/(?P<pk>\d+)\.(?P<format>[a-z0-9]+)$, views.IndexView.as_view({get:retrieve,delete:destroy,put:update,patch:partial_update})),

        class IndexView(viewsets.ModelViewSet):
            queryset = models.UserInfo.objects.all()
            serializer_class = IndexSerializer
            pagination_class = P2

    第三类:自动帮咱们生成四种url,index/$,index/(?P<pk>\d+)$,index/\.(?P<format>[a-z0-9]+)$,index/(?P<pk>\d+)\.(?P<format>[a-z0-9]+)$
        from rest_framework.routers import DefaultRouter   

        router = DefaultRouter()
        router.register(index,views.IndexViewSet)
        urlpatterns = [
            url(r^, include(router.urls)),
        ]
        
        
        class IndexViewSet(viewsets.ModelViewSet):
            queryset = models.UserInfo.objects.all()
            serializer_class = IndexSerializer
            pagination_class = P2
            
            
            
        class IndexSerializer(serializers.ModelSerializer):
            class Meta:
                model = models.UserInfo
                fields = "__all__"

 

以上是关于python-django rest framework框架之路由的主要内容,如果未能解决你的问题,请参考以下文章

python-django rest framework框架之路由

python-django rest framework框架之dispatch方法源码分析

Django REST framework 单元测试

注册后 Django Rest Framework 无法登录

将 django-rest-framework 中的超链接添加到 ModelViewSet

Angular 2 前端 django 2 REST 框架后端用户身份验证