• URL控制器


    自定义路由

    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对象 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
  • 相关阅读:
    网上找的Backbone.js
    关于数据结构,剑指offer上面的
    软件工程 什么叫高内聚 低耦合
    【丢失的转化率】你的宝贝,有多少人放进了购物车却没有支付?
    怎么都没人提 google 加密搜索呢? google如何稳定打开
    Mustache.js语法学习笔记
    C# POST数据到指定页面,并跳转至该页面
    将多个图片整合到一张图片中再用CSS 进行网页背景定位
    铁通、长宽网络支付时“签名失败”问题分析及解决方案  [88222001]验证签名异常:FAIL[20131101100002-142]
    jqGrid中多选
  • 原文地址:https://www.cnblogs.com/wangke0917/p/10638087.html
Copyright © 2020-2023  润新知