Django 路由系统
单一路由对应
from django.urls import re_path path
# FBV
re_path(r'^login.html$', views.login),
path(r'login.html', views.login),
# CBV
re_path(r'^login.html$', views.Login.as_view()),
基于正则的路由
from django.urls import re_path
re_path(r'^test-(\d+)/', views.test), # 以位置判断参数
re_path(r'^url-(?P<nid>\d+)-(?P<uid>\d+).html', views.url), # 以名称判断参数,建议使用这种,不受传递参数位置改变影响
获取方法:
def test1(request, page):
print(page)
return render(request, 'test1.html')
def test2(request, **kwargs):
print(kwargs.get('nid', None), kwargs.get('uid', None))
return render(request, 'test2.html')
添加额外的参数
# FBV
re_path(r'^index1.html$', views.index(参数)),
# CBV
re_path(r'^index1.html$', views.Index.as_view()(参数)),
为路由映射设置名称
re_path(r'^index1/', views.index, name='i1'),
re_path(r'^index2/(\d+)/(\d+)/', views.index, name='i2'),
re_path(r'^index3/(?P<pid>\d+)/(?P<nid>\d+)/', views.index, name='i3'),
在 .py 文件中
通过 reverse
获取 url
from django.urls import reverse
def func(request, *args, **kwargs):
url1 = reverse('i1') # index1/
url2 = reverse('i2', args=(1,2,)) # index2/1/2/
url3 = reverse('i3', kwargs={'pid': 1, 'nid': 9}) # index3/1/9/
在 html 文件中
{% url "i1" %} # index1/
{% url "i2" 1 2 %} # index2/1/2/
{% url "i3" pid=1 nid=9 %} # index3/1/9/
根据APP对路由规则进行分类
project/urls.py:
from django.conf.urls import re_path, include
from django.contrib import admin
urlpatterns = [
re_path(r'^app01/', include("app01.urls")),
re_path(r'^app02/', include("app02.urls")),
]
app01/urls.py:
from django.conf.urls import re_path, include
from django.contrib import admin
from app01 import views
urlpatterns = [
re_path(r'^login/', views.login),
]
app02/urls.py:
from django.conf.urls import re_path, include
from django.contrib import admin
from app02 import views
urlpatterns = [
re_path(r'^login/', views.login),
]
默认值
url.py:
urlpatterns = [
re_path(r'^index/', views.index, {'name': 'root'}),
]
views.py:
def index(request, name):
print(name)
return HttpResponse('OK')
命名空间
project/urls.py:
from django.conf.urls import re_path, include
urlpatterns = [
re_path(r'^app01-m/', include("app01.urls", namespace='app_m')),
re_path(r'^app01-n/', include("app01.urls", namespace='app_n')),
]
app01/urls.py:
from django.conf.urls import url
from app01 import views
app_name = 'app01'
urlpatterns = [
url(r'^(?P<pk>\d+)/$', views.test, name='detail')
]
在 views.py 文件中
from django.urls import reverse
def test(request, **kwargs):
pk = kwargs.get('pk', None)
print(reverse('app01:detail', kwargs={'pk': pk}))
print(reverse('app_m:detail', kwargs={'pk': pk}))
print(reverse('app_n:detail', kwargs={'pk': pk}))
print(request.resolver_match)
return render(request, 'test.html', {'pk': pk})
访问'/app01-m-app/1/',输出结果:
/app01-n/1/
/app01-m/1/
/app01-n/1/
ResolverMatch(func=app01.views.test, args=(), kwargs={'pk': '1'}, url_name=detail, app_names=['app01'], namespaces=['app_m'], route=^app01-m/(?P<pk>\d+)/$)
访问'/app01-n-app/1/',输出结果:
/app01-n/1/
/app01-m/1/
/app01-n/1/
ResolverMatch(func=app01.views.test, args=(), kwargs={'pk': '1'}, url_name=detail, app_names=['app01'], namespaces=['app_n'], route=^app01-n/(?P<pk>\d+)/$)
在 test.html 文件中
{% url 'app01:detail' pk %}
{% url 'app_m:detail' pk %}
{% url 'app_n:detail' pk %}
访问'/app01-m-app/1/',显示:
/app01-m/1/ /app01-m/1/ /app01-n/1/
访问'/app01-n-app/1/',显示:
/app01-n/1/ /app01-m/1/ /app01-n/1/