• rest-framework之url控制


    rest-farmwork之url控制

    一.基本路由(基本写法)

    • urls.py
    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()),
    ]
    
    • views.py
    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("")
    

    二.半自动化路由(必须继承ViewSetMixin)

    • urls.py
    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'})),
    
    ]
    
    • views.py
    from rest_framework.viewsets import ModelViewSet
    class PublishView(ModelViewSet):
        queryset=models.Publish.objects.all()
        serializer_class=PublishSerializers
    

    三.全自动路由(自动生成路由,必须继承ModelViewSet)

    • urls.py
    from django.conf.urls import url,include
    from app01 import views
    from rest_framework import routers
    
    # SimpleRouter 自动生成两条路由
    from rest_framework.routers import SimpleRouter
    router=SimpleRouter()
    router.register('publish',views.PublishView)
                    
    # DefaultRouter自动生成四条路由
    from rest_framework.routers import DefaultRouter
    router=DefaultRouter()
    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))
    ]
    
    • views.py
    from rest_framework.viewsets import ModelViewSet
    class PublishView(ModelViewSet):
        queryset=models.Publish.objects.all()
        serializer_class=PublishSerializers
    
  • 相关阅读:
    每周分享五个 PyCharm 使用技巧(一)
    深入理解 Python 中的上下文管理器
    Delphi中Chrome Chromium、Cef3学习笔记(四)
    DELPHI中自定义消息的发送和接收
    Delphi2010/XE2下隐藏程序系统任务栏的图标
    批处理经典入门教程!(从不懂到高手)第2/5页
    批处理经典入门教程!(从不懂到高手)1/5
    批处理taskkill运行结束不掉程序以及停留问题
    delphi EncdDecd.pas单元中Encoding方法出现#$D#$A的解决方法
    Delphi中Chrome Chromium、Cef3学习笔记(三)
  • 原文地址:https://www.cnblogs.com/majingjie/p/11138661.html
Copyright © 2020-2023  润新知