标签:site dex 系统 blank oct head *args none mail
1、单一路由对应
url(r‘^index/‘, views.index), # FBV
url(r‘^home/‘, views.Home.as_view()), # CBV
2、基于正则的路由
1 url(r‘^detail-(\d+).html‘, views.detail), 2 3 url(^‘^detail-(\d+)-(\d+).html‘,views.detail), 4 ‘‘‘ 5 严格按照位置参数的顺序传入参数,如果在实际环境中,def detail(request,nid,uid)在定义时,参数的指定出现差错,则会导致下面的对参数的引用出现错误。 6 ‘‘‘ 7 8 url(r‘^detail-(?P<nid>\d+)-(?P<uid>\d+).html‘, views.detail), 9 ‘‘‘ 10 传参时确保了nid=第一个传入的位置参数,uid=第二个传入的位置参数。 11 ‘‘‘
实例1:实现 ?nid={{ id }}的url:
1 from django.conf.urls import url 2 from django.contrib import admin 3 from app01 import views 4 5 urlpatterns = [ 6 url(r‘^admin/‘, admin.site.urls), 7 url(r‘^login‘,views.login), 8 url(r‘^index‘,views.index), 9 url(r‘^detail‘,views.detail), 10 # url(r‘^detail-(\d+).html‘,views.detail), 11 url(r‘^home‘,views.Home.as_view()), #对应类Home的对应方法 12 ]
1 USER_DICT={ 2 ‘1‘:{‘name‘:‘root1‘,‘email‘:‘root@live.com‘}, 3 ‘2‘:{‘name‘:‘root2‘,‘email‘:‘root@live.com‘}, 4 ‘3‘:{‘name‘:‘root3‘,‘email‘:‘root@live.com‘}, 5 ‘4‘:{‘name‘:‘root4‘,‘email‘:‘root@live.com‘}, 6 ‘5‘:{‘name‘:‘root5‘,‘email‘:‘root@live.com‘}, 7 } 8 9 def detail(request): 10 nid=request.GET.get(‘nid‘,None) 11 detail_info=USER_DICT[nid] 12 return render(request,‘detail.html‘,{‘detail_info‘:detail_info})
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <h1>详细信息</h1> 9 <h6>用户名:{{ detail_info.name }}</h6> 10 <h6>邮箱:{{ detail_info.email }}</h6> 11 </body> 12 </html>
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 9 <ul> 10 {% for k,v in user_dict.items %} 11 <li><a target=_blank href="/detail?nid={{ k }}">{{ v.name }}</a></li> 12 {% endfor %} 13 </ul> 14 15 </body> 16 </html>
实例2:实现url-{{ id }} 以这种方式用正则表达式取页面。
1 from django.conf.urls import url 2 from django.contrib import admin 3 from app01 import views 4 5 urlpatterns = [ 6 url(r‘^admin/‘, admin.site.urls), 7 url(r‘^login‘,views.login), 8 url(r‘^index‘,views.index), 9 # url(r‘^detail‘,views.detail), 10 url(r‘^detail-(\d+).html‘,views.detail), 11 url(r‘^home‘,views.Home.as_view()), #对应类Home的对应方法 12 ]
1 USER_DICT={ 2 ‘1‘:{‘name‘:‘root1‘,‘email‘:‘root@live.com‘}, 3 ‘2‘:{‘name‘:‘root2‘,‘email‘:‘root@live.com‘}, 4 ‘3‘:{‘name‘:‘root3‘,‘email‘:‘root@live.com‘}, 5 ‘4‘:{‘name‘:‘root4‘,‘email‘:‘root@live.com‘}, 6 ‘5‘:{‘name‘:‘root5‘,‘email‘:‘root@live.com‘}, 7 } 8 9 def detail(request,nid): 10 # nid=request.GET.get(‘nid‘,None) 11 detail_info=USER_DICT[nid] 12 return render(request,‘detail.html‘,{‘detail_info‘:detail_info})
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <h1>详细信息</h1> 9 <h6>用户名:{{ detail_info.name }}</h6> 10 <h6>邮箱:{{ detail_info.email }}</h6> 11 </body> 12 </html>
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 9 <ul> 10 {% for k,v in user_dict.items %} 11 <li><a target=_blank href="/detail-{{ k }}.html">{{ v.name }}</a></li> 12 {% endfor %} 13 </ul> 14 15 </body> 16 </html>
3、为路由映射设置名称
对URL路由关系进行命名, ***** 以后可以根据此名称生成自己想要的URL *****
1 url(r‘^asdfasdfasdf/‘, views.index, name=‘i1‘), 2 对应在index.html中: 3 <form action="{% url "i1" %}" method="POST"> 4 5 url(r‘^index/(\d+)/(\d+)/‘, views.index, name=‘i2‘), 6 对应在index.html中: 7 <form action="{% url "i2" 1 2 %}" method="POST"> 8 9 url(r‘^index/(?P<pid>\d+)/(?P<nid>\d+)/‘, views.index, name=‘i3‘), 10 对应在index.html中: 11 <form action="{% url "i3" pid=1 nid=9 %}" method="POST"> 12 13 # 获取当前的URL 14 request.path_info 15 16 17 # 在函数中生成URL 18 def func(request, *args, **kwargs): 19 from django.urls import reverse 20 21 url1 = reverse(‘i1‘) # asdfasdfasdf/ 22 url2 = reverse(‘i2‘, args=(1,2,)) # index/1/2/ 23 url3 = reverse(‘i3‘, kwargs={‘pid‘: 1, "nid": 9}) # index/1/9/
4、多级路由:根据app对路由规则进行分类
客户端请求先到project/urls.py中进行匹配,匹配到下面的各个app的app/urls.py中。
1 from django.conf.urls import url,include 2 from django.contrib import admin 3 4 urlpatterns = [ 5 url(r‘^cmdb/‘, include("app01.urls")), 6 url(r‘^monitor/‘, include("app02.urls")), 7 ]
1 from django.conf.urls import url,include 2 from django.contrib import admin 3 from app01 import views 4 5 urlpatterns = [ 6 url(r‘^login/‘, views.login), 7 ]
1 from django.conf.urls import url,include 2 from django.contrib import admin 3 from app02 import views 4 5 urlpatterns = [ 6 url(r‘^login/‘, views.login), 7 ]
5、默认值(欠)
添加额外的参数
6、命名空间(欠)
标签:site dex 系统 blank oct head *args none mail
原文地址:http://www.cnblogs.com/zoe233/p/7610774.html