【Django】url(路由系统)

时间:2022-09-12 19:15:55

 

 

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.默认值
 
【Django】url(路由系统)【Django】url(路由系统)
urlpatterns = [

    url(r'^index/',views.index,{'name':'root'}),

]
project/urls.py
【Django】url(路由系统)【Django】url(路由系统)
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)
app01/views.py

 

4.路由分发

【Django】url(路由系统)【Django】url(路由系统)
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")),
]
project/urls.py
【Django】url(路由系统)【Django】url(路由系统)
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()),
]
app01/urls.py
【Django】url(路由系统)【Django】url(路由系统)
from django.contrib import admin
from app02 import views
from django.conf.urls import url

urlpatterns = [
    url('^login/', views.login),
]
app02/urls.py

 

5.命名空间
【Django】url(路由系统)【Django】url(路由系统)
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')),
]
project/urls.py
【Django】url(路由系统)【Django】url(路由系统)
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'),
]
app01/urls.py
【Django】url(路由系统)【Django】url(路由系统)
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')
app01/views.py

【Django】url(路由系统)

【Django】url(路由系统)