DRF的Router的简单用法

  这篇博客的例子:DRF的视图

  出版社的视图函数不变,路由按照如下写,也能实现对应的功能:

from django.urls import path,re_path

from book import views

urlpatterns = [
    re_path(r'^books/',views.BookView.as_view(),name='books'),
    re_path(r'^book/(\d+)',views.BookDetail.as_view(),name='book_detail'),

    # re_path(r'^publishers/$',views.PublishList.as_view()),
    # # 使用组合类的话这里必须加上有名分组!pk是查询的条件!
    # re_path(r'^publishers/(?P<pk>\d+)/$',views.PublishDetail.as_view()),

    # re_path(r'^publishers/$',views.PublishView.as_view(actions={"get":"list","post":"create"})),
    # # 使用组合类的话这里必须加上有名分组!pk是查询的条件!
    # re_path(r'^publishers/(?P<pk>\d+)/$',views.PublishView.as_view(actions={"get":"retrieve","put":"update","delete":"destroy"})),

]

from rest_framework.routers import DefaultRouter

router = DefaultRouter()
# router.register('books', views.BookListView)
router.register('publishes', views.PublishView)
urlpatterns += router.urls

 

posted on 2019-08-15 15:39  江湖乄夜雨  阅读(591)  评论(0编辑  收藏  举报