drf-ViewSet之路由简写的方法

https://www.bilibili.com/video/BV1z5411D7BQ?p=20&spm_id_from=pageDriver&vd_source=caabcbd2a759a67e2a3de8acbaaf08ea

url.py

from django.contrib import admin
from django.urls import path,re_path

# 应用的视图层
from sers import views
from django.urls import re_path as url
from rest_framework import routers

#1、注册路由进去
router=routers.DefaultRouter()
router.register('book',views.BookView)


urlpatterns = [
    path('admin/', admin.site.urls),

#     # 新
#     url('^book/$', views.BookView.as_view({"get":"list","post":"create"})),
#     # 新
#     # 加有名分组?P<pk>
#     url('^book/(?P<pk>\d+)/$', views.BookView.as_view({"get":"retrieve","put":"update","delete":"destroy"})),
]
#2、 把路由驾到路由列表里即可,就自动生成了上面2条路由
urlpatterns += router.urls

 

主要是下面这些

from rest_framework import routers

#1、注册路由进去
router=routers.DefaultRouter()
router.register('book',views.BookView)

urlpatterns = []
 
# 把路由驾到路由列表里即可,就自动生成了上面2条路由
urlpatterns += router.urls
 
 
 

 

posted @ 2022-10-27 17:54  凯宾斯基  阅读(27)  评论(0编辑  收藏  举报