Django入門2:路由系統
阿新 • • 發佈:2017-09-29
-c clas sdfas ret war 3-0 生成 實例 eee
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‘urls.py:^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‘,‘views.py:email‘:‘[email protected]‘}, 3 ‘2‘:{‘name‘:‘root2‘,‘email‘:‘[email protected]‘}, 4 ‘3‘:{‘name‘:‘root3‘,‘email‘:‘[email protected]‘}, 5 ‘4‘:{‘name‘:‘root4‘,‘email‘:‘[email protected]‘}, 6 ‘5‘:{‘name‘:‘root5‘,‘email‘:‘[email protected]‘}, 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>detail.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>index.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 ]urls.py:
1 USER_DICT={ 2 ‘1‘:{‘name‘:‘root1‘,‘email‘:‘[email protected]‘}, 3 ‘2‘:{‘name‘:‘root2‘,‘email‘:‘[email protected]‘}, 4 ‘3‘:{‘name‘:‘root3‘,‘email‘:‘[email protected]‘}, 5 ‘4‘:{‘name‘:‘root4‘,‘email‘:‘[email protected]‘}, 6 ‘5‘:{‘name‘:‘root5‘,‘email‘:‘[email protected]‘}, 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})views.py:
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>detail.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>index.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 ]project/urls.py
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 ]app01/urls.py
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 ]app02/urls.py
5、默認值(欠)
添加額外的參數
6、命名空間(欠)
Django入門2:路由系統