1、单一路由对应
url(r'^index/',views.index),
2.基于正则的路由
url(r
'^index/(\d*)'
, views.index),
url(r
'^manage/(?P<name>\w*)/(?P<id>\d*)'
, views.manage),
3.默认值
urlpatterns = [ url(r'^index/',views.index,{'name':'root'}), ]
from django.shortcuts import render,HttpResponse,redirect from django.urls import reverse # Create your views here. def index(request,name): print(name) return HttpResponse(name)
4.路由分发
from django.contrib import admin from django.conf.urls import url,include urlpatterns = [ url('^admin/', admin.site.urls), url(r'^app01/',include("app01.urls")), url(r'^app02/',include("app02.urls")), ]
from django.contrib import admin from django.conf.urls import url from app01 import views urlpatterns = [ url('^index/', views.index), url('^user_info/', views.user_info), url('^userdetail-(?P<nid>\d+)/', views.user_detail), url('^home/', views.Home.as_view()), ]
from django.contrib import admin from app02 import views from django.conf.urls import url urlpatterns = [ url('^login/', views.login), ]
5.命名空间
from django.contrib import admin from django.conf.urls import url,include from app01 import views urlpatterns = [ url(r'^app01/', include('app01.urls', namespace='author')), ]
from django.contrib import admin from django.conf.urls import url,include from app01 import views app_name = 'app01' urlpatterns = [ url(r'^index/',views.index,name='daly'), ]
from django.shortcuts import render,HttpResponse,redirect from django.urls import reverse # Create your views here. def index(request): v = reverse('author:daly') print(v) return HttpResponse('OK')