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.默认值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
urlpatterns = [ url(r'^index/',views.index,{'name':'root'}), ]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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.路由分发
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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")), ]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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()), ]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.contrib import admin from app02 import views from django.conf.urls import url urlpatterns = [ url('^login/', views.login), ]
5.命名空间
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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')), ]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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'), ]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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')