码迷,mamicode.com
首页 > 编程语言 > 详细

python-django rest framework框架之路由

时间:2018-02-27 01:17:47      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:djang   pos   url   dex   http   rgs   efault   self   api   

路由
    
    第一类:原始继承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框架之路由

标签:djang   pos   url   dex   http   rgs   efault   self   api   

原文地址:https://www.cnblogs.com/liuwei0824/p/8476410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!