URL路由器

一、 自定义路由

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^categories/$',views.CategoriesView.as_view()),
    url(r'^ categories\.(?P<format>\w+)$',views Categories View.as_view()),
    url(r'^ categories /(?P<pk>\d+)/$',views. Categories View.as_view()),
    url(r'^ categories /(?P<pk>\d+)\.(?P<format>\w+)$',views. Categories View.as_view()), ]

二:半自动路由

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^ categories /$', views.CategoriesView.as_view({
                                         "get": "list", 
                                         "post": "create"
                                }), name=" categories "),
    url(r'^ categories /(?P<pk>\d+)/$', views.CategoriesView.as_view({
                                       'get': 'retrieve',
                                       'put': 'update',
                                       'patch': 'partial_update',
                                       'delete': 'destroy'
}),]

三:全自动路由

from django.conf.urls import url,include
from django.contrib import admin
from app01 import views
from rest_framework.routers import DefaultRouter
route = DefaultRouter()
route.register('xxxxx',views.RouteView)
----------------------------
本质如下四种:
url(r'^xxxxx/', views.RouteView.as_view({'get':'list','post':'create'})),
url(r'^xxxxx\.(?P<format>\w+)', views.RouteView.as_view({'get':'list','post':'create'})),
url(r'^xxxxx/(?P<pk>\d+)',views.RouteView.as_view({'get':'retrieve','delete':'destroy','put':'update'})),
url(r'^xxxxx/(?P<pk>\d+)\.(?P<format>\w+)',views.RouteView.as_view({'get':'retrieve','delete':'destroy','put':'update'})),
-----------------------------
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include(route.urls)),
]
posted @ 2018-08-07 07:11  桥前石头  阅读(156)  评论(0编辑  收藏  举报