s14_02_路由系统
路由系统
- 单一路由对应
url(r'^index$', views.index),
# url.py
# from app01 import views
# "login" --> 函数名
# 此处r'^login/'中的/ 与form中的action="/login/" 保持一致
# RuntimeError at /login
1、url(r'^index/', views.index),
url(r'^home/', views.Home.as_view()),
2、url(r'^detail-(\d+).html', views.detail),
def detail(request,nid):
3、url(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html', views.detail)
def detail(request, *args,**kwargs):
pass
示例:
a.
url(r'^detail-(\d+)-(\d+).html', views.detail),
def func(request, nid, uid):
pass
def func(request, *args):
args = (2,9)
def func(request, *args, **kwargs):
args = (2,9)
b.
url(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html', views.detail)
def func(request, nid, uid):
pass
def funct(request, **kwargs):
kwargs = {'nid': 1, 'uid': 3}
def func(request, *args, **kwargs):
args = (2,9)
- 字典嵌套,查看详细,URL正则
USER_DICT = {
'1':{'name':'root1','email':'root@live.com'},
'2':{'name':'root2','email':'root@live.com'},
'3':{'name':'root3','email':'root@live.com'},
'4':{'name':'root4','email':'root@live.com'},
'5':{'name':'root5','email':'root@live.com'},
}
# def detail(request):
# nid = request.GET.get('nid')
# detail_info=USER_DICT[nid]
# return render(request,'detail.html',{'detail_info':detail_info})
def detail(request,nid):
"""
# return HttpResponse(nid)
print(nid,uid)
return HttpResponse(nid,uid)
# uid 无法返回
"""
detail_info=USER_DICT[nid]
return render(request,'detail.html',{'detail_info':detail_info})
# detail('req',1,2)
# detail('req',nid=1,uid=3)
# def detail(request,*args,**kwargs):
# url(r'^detail/', views.detail),
# url(r'^detail-(\d+).html/', views.detail),
# url(r'^detail-(\d+)-(\d+).html/', views.detail),
# url(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html/', views.detail),
{% for k,row in user_dict.items %}
//<li><a target="_blank" href="/detail/?nid={{ k }}">{{ row }}</a></li>
//<li><a target="_blank" href="/detail/?nid={{ k }}">{{ row.name }}</a></li>
<li><a target="_blank" href="/detail-{{ k }}.html">{{ row.name }}</a></li>
{% endfor %}
4、 name
对URL路由关系进行命名,以后可以根据此名称生成自己想要的URL
url(r'^asdfasdfasdf/', views.index, name='i1'),
url(r'^yug/(\d+)/(\d+)/', views.index, name='i2'),
url(r'^buy/(?P<pid>\d+)/(?P<nid>\d+)/', views.index, name='i3'),
def func(request, *args, **kwargs):
from django.urls import reverse
url1 = reverse('i1') # asdfasdfasdf/
url2 = reverse('i2', args=(1,2,)) # yug/1/2/
url3 = reverse('i3', kwargs={'pid': 1, "nid": 9}) # buy/1/9/
xxx.html
{% url "i1" %} # asdfasdfasdf/
{% url "i2" 1 2 %} # yug/1/2/
{% url "i3" pid=1 nid=9 %} # buy/1/9/
注:
# 当前的URL
request.path_info
- 示例:
# url(r'^xxxxxxxxxxxxxxxx/', views.index,name='indexx'),
# url(r'^index/(\d+)/(\d+)/', views.index,name='indexx'),
url(r'^index/(?P<nid>\d+)/(?P<uid>\d+)/', views.index,name='indexx'),
//<form action="{% url 'indexx' %}" method="POST">#}
//<form action="{% url 'indexx' 4 %}" method="POST">#}
//<form action="{{ request.path_info }}" method="POST">#}
// 添加完后留在当期页
<form action="{% ur 'indexx' nid=1,uid=3 %}" method="POST">#}
<input type="text"name="user">#}
<input type="text"name="email">#}
<input type="submit">#}
</form>#}
- 反向生成URL
def index(request,nid,uid):
print(request.path_info)
# /xxxxxxx/33333333
from django.urls import reverse
# v = reverse('indexx')
# v = reverse('indexx',args=(90,80,))
v = reverse('indexx',kwargs={'nid':11,'uid':99})
print(v)
return render(request,'index.html',{'user_dict':USER_DICT})
5、多级路由
project/urls.py
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^cmdb/', include("app01.urls")),
url(r'^monitor/', include("app02.urls")),
]
app01/urls.py
from django.conf.urls import url,include
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^login/', views.login),
]
app02/urls.py
from django.conf.urls import url,include
from django.contrib import admin
from app02 import views
urlpatterns = [
url(r'^login/', views.login),
]
- 示例:
# project/urls.py
from django.conf.urls import url,include
from django.contrib import admin
# from app01 import views as V1
# from app02 import views as V2
# from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^cmdb/', include('app01.urls')),
url(r'^monitor/', include('app02.urls')),
]
# app01/urls.py
from django.conf.urls import url,include
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^login/', views.login),
]
6、默认值
url(r'^index/', views.index, {'name': 'root'}),
def index(request,name):
print(name)
return HttpResponse('OK')
7、命名空间
a. project.urls.py
from django.conf.urls import url,include
urlpatterns = [
url(r'^a/', include('app01.urls', namespace='author-polls')),
url(r'^b/', include('app01.urls', namespace='publisher-polls')),
]
b. app01.urls.py
from django.conf.urls import url
from app01 import views
app_name = 'app01'
urlpatterns = [
url(r'^(?P<pk>\d+)/$', views.detail, name='detail')
]
c. app01.views.py
from django.shortcuts import render,HttpResponse
from django.urls import reverse
def detail(request, pk):
print(request.resolver_match)
return HttpResponse(pk)
以上定义带命名空间的url之后,使用name生成URL时候,应该如下:
v = reverse('author-polls:detail', kwargs={'pk':11})
{% url 'author-polls:detail' pk=12 pp=99 %}
# django中的路由系统和其他语言的框架有所不同,在django中每一个请求的url都要有一条路由映射,
# 这样才能将请求交给对一个的view中的函数去处理。
# 其他大部分的Web框架则是对一类的url请求做一条路由映射,从而是路由系统变得简洁。
# 通过反射机制,为django开发一套动态的路由系统Demo: 点击下载:django_dynamic_router.rar