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__"

 

posted @ 2018-02-26 22:37  Cool·  阅读(813)  评论(0编辑  收藏  举报