• rest-framework之url控制


    一 自定义路由(原始方式)

    二 半自动路由(视图类继承ModelViewSet)

    三 全自动路由(自动生成路由)

    ###

    一 自定义路由(原始方式)

    from django.conf.urls import url
    from app01 import views
    urlpatterns = [
        url(r'^books/$', views.BookView.as_view()),
        url(r'^books/(?P<pk>d+)$', views.BookDetailView.as_view()),
    ]
    复制代码
    class BookView(APIView):
    
        def get(self, request):
            book_list = models.Book.objects.all()
            bs = BookSerializers(book_list, many=True)
            return Response(bs.data)
    
        def post(self, request):
            # 添加一条数据
            print(request.data)
    
            bs=BookSerializers(data=request.data)
            if bs.is_valid():
                bs.save()  # 生成记录
                return Response(bs.data)
            else:
    
                return Response(bs.errors)
    
    class BookDetailView(APIView):
        def get(self,request,pk):
            book_obj=models.Book.objects.filter(pk=pk).first()
            bs=BookSerializers(book_obj,many=False)
            return Response(bs.data)
        def put(self,request,pk):
            book_obj = models.Book.objects.filter(pk=pk).first()
    
            bs=BookSerializers(data=request.data,instance=book_obj)
            if bs.is_valid():
                bs.save() # update
                return Response(bs.data)
            else:
                return Response(bs.errors)
        def delete(self,request,pk):
            models.Book.objects.filter(pk=pk).delete()
    
            return Response("")
    复制代码
     

    二 半自动路由(视图类继承ModelViewSet)

    复制代码
    from django.conf.urls import url
    from app01 import views
    urlpatterns = [
        url(r'^publish/$', views.PublishView.as_view({'get':'list','post':'create'})),
        url(r'^publish/(?P<pk>d+)/$', views.PublishView.as_view({'get':'retrieve','put':'update','delete':'destroy'})),
    
    ]
    复制代码
    from rest_framework.viewsets import ModelViewSet
    class PublishView(ModelViewSet):
        queryset=models.Publish.objects.all()
        serializer_class=PublishSerializers
     

    三 全自动路由(自动生成路由)

    复制代码
    from django.conf.urls import url,include
    from app01 import views
    from rest_framework import routers
    router=routers.DefaultRouter()
    # 两个参数,一个是匹配的路由,一个是视图中写的CBV的类
    router.register('publish',views.PublishView)
    urlpatterns = [
        # http://127.0.0.1:8000/publish/format=json(渲染器通过这个判断,返回渲染的页面)
        # url(r'^publish/', views.PublishView.as_view({'get':'list','post':'create'})),
        # http://127.0.0.1:8000/publish.json(渲染器通过这个判断,返回渲染的页面)
        # url(r'^publish.(?P<format>w+)$', views.PublishView.as_view({'get':'list','post':'create'})),
        
        # 可以用 以下方式访问
        # 1 http://127.0.0.1:8000/publish/
        # 2 http://127.0.0.1:8000/publish.json
        # 3 http://127.0.0.1:8000/publish/3
        # 4 http://127.0.0.1:8000/publish/3.json   
        url(r'',include(router.urls))
    ]
    复制代码
    from rest_framework.viewsets import ModelViewSet
    class PublishView(ModelViewSet):
        queryset=models.Publish.objects.all()
        serializer_class=PublishSerializers

    ###

  • 相关阅读:
    C#中datagridview单元格值改变实现模糊查找
    C#中建立treeView
    L2008 最长对称子串
    第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明)
    GPLT团体程序设计天梯赛练习集 L1041~L1050
    GPLT团体程序设计天梯赛练习集 L1051~L1060
    GPLT团体程序设计天梯赛练习集 L1061~L1072
    第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明)(热身赛)
    L2007 家庭房产 并查集
    利用反射把查询到的Table、Reader转换成List、Model
  • 原文地址:https://www.cnblogs.com/xujinjin18/p/9790945.html
Copyright © 2020-2023  润新知