一、位置分組與關(guān)鍵字分組
創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務領(lǐng)域包括:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的云岡網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設(shè)合作伙伴!位置分組:
-按位置傳參
-分組之后,會把分組出來的數(shù)據(jù),當位置參數(shù),傳到視圖函數(shù),所以,視圖函數(shù)需要定義形參
urls.py
# 精確匹配
url(r'^publish/$', views.publish)
views.py
def publish(request): if request.method=='GET': return HttpResponse("This is get") elif request.method=='POST': return HttpResponse('This is POST')
# 訪問:http://127.0.0.1:8000/publish/
This is get
urls.py
# 匹配publish 后四位數(shù)字
url(r'^publish/[0-9]{4}/$', views.publish)
# 匹配任意長度的數(shù)字
url(r'^publish/\d+/$',views.publish)
views.py
同上面一樣
urls.py
# 配后面的publish/數(shù)字4位/與數(shù)字2位 ,其他均為404 (注:多個分組中都帶有小括號)
url(r'^publish/([0-9]{4})/([0-9]{2})/$',views.publish),
views.py
def publish(request,year,month): # 有括號(分組)的必須要傳參數(shù)過去 if request.method=='GET': return HttpResponse(' This is get') elif request.method=='POST': return HttpResponse('This is POST')
視圖函數(shù)還可以這樣寫(*args):
def publish(request,*args): if request.method == 'GET': return HttpResponse(' This is get') elif request.method == 'POST': return HttpResponse('This is POST')
訪問:http://127.0.0.1:8000/publish/1234/20/
This is get
urls.py
# 有括號(分組)的必須要傳參數(shù)過去,publish后面匹配多個數(shù)字
url(r'^publish/(\d+)/$',views.publish),
views.py
def publish(request,year): return HttpResponse("publish")
訪問:http://127.0.0.1:8000/publish/任意數(shù)字
關(guān)鍵字分組:
-按關(guān)鍵字傳參
-有名分組之后,會把分組出來的數(shù)據(jù),當關(guān)鍵字參數(shù),傳到視圖函數(shù),所以,視圖函數(shù)需要定義形參,形參名字要跟分組的名字對應,與順序無關(guān)
urls.py
# 關(guān)鍵字分組是按照關(guān)鍵字傳參數(shù)
url(r'^publish/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$',views.publish),
views.py
def publish(request,year,month): # 有括號(分組)的必須要傳參數(shù)過去 if request.method=='GET': return HttpResponse(' This is get') elif request.method=='POST': return HttpResponse('This is POST')
urls.py
# 位置分組與關(guān)鍵字分組混合使用
url(r'^publish/([0-9]{4})/(?P<month>[0-9]{2})/$',views.publish),
views.py
# 位置分組與關(guān)鍵字分組混合使用,可以使用*args,**kwargs 接收 (建議不要混著用)
def publish(request,*args,**kwargs): if request.method=='GET': print(args,kwargs) # () {'month': '12'} return HttpResponse(' This is get publish') elif request.method=='POST': return HttpResponse('This is POST')
二、反向解析
# 分組
作用:
例如:當我訪問http://127.0.0.1:8000/publish/ 去點擊某一個鏈接的時候,去訪問到了http://127.0.0.1:8000/publishadd/
urls.py
url(r'^publish/$',views.publish), url(r'^publishadd/$',views.publishadd,name='pub'),
# 若以后要去到這個地址(publishadd)會發(fā)生改變的話,后面可以加上‘name=自已定義的名字與pub.html網(wǎng)頁標簽中定義的名字一致’,這樣,publishadd這個路由再如何變化,訪問到/publish/中指定某一個鏈接時,也還是會去到你已改變的那個路由上的。(類似給publishadd,取了一個別名)
views.py
def publish(request): if request.method=='GET': return render(request, 'pub.html') def publishadd(request): return HttpResponse('publishadd')
templates/pub.html
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>反向解析</title> </head> <body> {#位置分組#} <a href="{% url 'pub'%}">點我去publishadd</a> </body> </html>
對于redirect 重定向的解決方案:
沒改前:
# 當我訪問:http://127.0.0.1:8000/publish,它會幫我跳轉(zhuǎn)到http://127.0.0.1:8000/publishadd/這個頁面中,但我路由層的publishadd換了其他名字的時候,便會報錯了。
urls.py
url(r'^publish/$',views.publish), url(r'^publishadd/$',views.publishadd,name='pub'),
views.py
def publish(request): if request.method=='GET': return redirect('/publishadd/') def publishadd(request): return HttpResponse('publishadd')
templates/pub.html
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>反向解析</title> </head> <body> {#位置分組#} <a href="{% url 'pub'%}">點我去publishadd</a> {#<a href="{% url 'pub' year=2018 month=12 %}">test去點我</a>#} </body> </html>
改之后:路由中的publishadd后面再如何改也會訪問到views.py文件中的publishadd這個函數(shù)屬性中
例如:訪問http://127.0.0.1:8000/publish,路由層publishadd改成了publishadd777,它也會訪問到publishadd這個函數(shù)的屬性中
urls.py
url(r'^publish/$',views.publish), url(r'^publishadd/$',views.publishadd,name='pub'),
views.py
from django.shortcuts import render,HttpResponse,redirect,reverse def publish(request): if request.method=='GET': url=reverse('pub') # 改之后,reverse反向解析的模塊 return redirect(url) # 改之后 def publishadd(request): return HttpResponse('publishadd')
templates/pub.html 不變
# 位置分組,參數(shù)傳遞
urls.py
url(r'^publish/$',views.publish), url(r'^publishadd/([0-9]{4})/$',views.publishadd,name='pub'), # 如果publishadd后面是兩個參數(shù)的話,views.py中的也需要跟著加
views.py
def publish(request): if request.method=='GET': return render(request, 'pub.html') def publishadd(request,year): # urls.py 中的publishadd后面要加2個參數(shù)的話,這里也要跟著加上2個參數(shù),例如:def publishadd(request,year,month): return HttpResponse('publishadd')
templates/pub.html
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>反向解析</title> </head> <body> {#位置分組#} <a href="{% url 'pub' 2018 %}">點我去publishadd</a> {# 如果上面是兩個參數(shù)的話 2018的后面還需要多傳一個參數(shù) #} </body> </html>
訪問:http://127.0.0.1:8000/publish/
點擊頁面中的 "點我去publishadd"
訪問到:http://127.0.0.1:8000/publishadd/2018/
顯示:publishadd 成功。
使用reverse模塊重定向
urls.py
url(r'^publish/$',views.publish), url(r'^publishadd/([0-9]{4})/([0-9]{2})/$',views.publishadd,name='pub'),
views.py
from django.shortcuts import render,HttpResponse,redirect,reverse def publish(request): if request.method=='GET': url=reverse('pub',args=(2018,12,)) return redirect(url) def publishadd(request,year,month): return HttpResponse('publishadd')
templates/pub.html
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>反向解析</title> </head> <body> {#位置分組#} <a href="{% url 'pub' 2018 02 %}">點我去publishadd</a> </body> </html>
訪問:http://127.0.0.1:8000/publish
自動跳轉(zhuǎn)到:http://127.0.0.1:8000/publishadd/2018/12/
顯示:publishadd 成功。
# 關(guān)鍵字分組 --- 模板層
urls.py
url(r'^publish/$',views.publish), url(r'^publishadd/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.publishadd, name='pub'),
views.py
def publish(request): if request.method=='GET': return render(request, 'pub.html') def publishadd(request,year,month): return HttpResponse('publishadd')
templates/pub.html
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>反向解析</title> </head> <body> {#關(guān)鍵字分組#} <a href="{% url 'pub' year=2018 month=12 %}">點我去publishadd</a> </body> </html>
訪問:http://127.0.0.1:8000/publish/
點擊 :點我去publishadd
訪問到:http://127.0.0.1:8000/publishadd/2018/12/
顯示:publishadd 成功~
# 關(guān)鍵字視圖層
urls.py 與 templates/pub.html 內(nèi)容不變(與上面的一致)
views.py
def publish(request): if request.method=='GET': url=reverse('pub',args=(2018,12,)) # url=reverse('pub',kwargs={'month':12,"year":2018}) # 兩種都支持,效果一樣。 return redirect(url) def publishadd(request,year,month): return HttpResponse('publishadd')
訪問:http://127.0.0.1:8000/publish
會重定向到:http://127.0.0.1:8000/publishadd/2018/12/
顯示 : publishadd !~~~成功
總結(jié):
-先命一個名: -1 無參數(shù):url(r'^publishadd133/$', views.publishadd,name='ddd'), -2 位置分組:url(r'^publishadd/([0-9]{4})/([0-9]{2})/$', views.publishadd,name='ddd'), -3 關(guān)鍵字分組:url(r'^publishadd/(?P<year>[0-9]{4})/(?P<mounth>[0-9]{2})/$', views.publishadd,name='ddd'), -在模板層: -1 無參數(shù):{% url 'ddd' %} -2 位置分組的:{% url 'ddd' 2018 12 %} -3 關(guān)鍵分組:{% url 'ddd' 2018 12 %} 還可以 {% url 'ddd' year=2018 mounth=12 %} -在視圖層: from django.shortcuts import reverse 在視圖函數(shù)里: 1 無參數(shù):url=reverse('ddd') 2 位置分組:url=reverse('ddd',args=(2018,12,)) 如果只有一個參數(shù),后面必須要加一個逗號 3 關(guān)鍵字分組:url=reverse('ddd',args=(2018,12,)) 還可以 url=reverse('ddd',kwargs={'year':2018,'mounth':12})
三、路由分發(fā)
作用:由總路由urls.py中先分發(fā),然后再到不同的app中由它們再次分發(fā)。
1、首先要再次手動創(chuàng)建一個app
命令:python3 manage.py startapp appname (我這里就叫blog了,另一個為app01)
2、在settings.py中注冊
INSTALLED_APPS 的列表中添加
'blog.apps.BlogConfig',
3、在每個app的目錄下面創(chuàng)建一個名為urls.py的文件。內(nèi)容為:
app01目錄下的urls.py中:
from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^app01_test/$',views.test), ]
app01目錄下的視圖函數(shù)views.py文件中:
def app01(request): return HttpResponse('app01--test')
blog目錄下的urls.py中:
from django.conf.urls import url from blog import views urlpatterns = [ url(r'^blog_test/$',views.test) ]
blog目錄下的視圖函數(shù)views.py文件中:
def blog(request): return HttpResponse('blog----test')
4、在總路由中的urls.py中的需要配置以下:
from django.conf.urls import url,include # 總路由中添加: url(r'^app01/',include('app01.urls')), url(r'^blog/',include('blog.urls')),
5、訪問:
http://127.0.0.1:8000/blog/blog_test/
http://127.0.0.1:8000/app01/app01_test/
總結(jié):
路由分發(fā) 1 在不同的app里創(chuàng)建urls.py 2 在總路由 -from django.conf.urls import include -url(r'^blog/',include('blog.urls')), -url(r'^app01/',include('app01.urls')), 3 在不同的app的urls里配置路由關(guān)系 ***重點***總路由,不能加結(jié)束符$
四、名稱空間
作用:以防止兩個應用的子路由后面的name='名字'相同或者怕沖突,這樣是為了讓它們自已找到屬于自已的名稱空間,以防沖突(或者不要把子路由后面的name='名字'名字命名成一樣的也可。做了解)
1、總路由urls.py
url(r'^app01/',include('app01.urls',namespace='app01')), url(r'^blog/',include('blog.urls',namespace='blog')),
app01 下的文件:
app01下的urls.py
from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^app01_test/$',views.app01,name='test'), ]
app01下的views.py
def app01(request): url=reverse('app01:test') print(url) return render(request,'app01.html')
templates/app01.html
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>反向解析</title> </head> <body> <a href="{% url 'app01:test' %}">app01_test</a> </body> </html>
blog下的文件:
blog下的urls.py
from django.conf.urls import url from blog import views urlpatterns = [ url(r'^blog_test/$',views.blog,name='test') ]
blog下的views.py
def blog(request): url=reverse('blog:test') print(url) return render(request,'blog.html')
templates/blog.html
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>反向解析</title> </head> <body> <a href="{% url 'blog:test' %}">blog_test</a> </body> </html>
五、偽靜態(tài)
偽靜態(tài)是相對真實靜態(tài)來講的,使用*.html的話,搜索引擎會認為這些頁面不太會經(jīng)常改動,被收錄的可能性就會大,而搜索關(guān)鍵字的時候,就會把你的頁面通過搜索引擎搜索出來,而其實這些頁面都是通過數(shù)據(jù)庫中查找出來的頁面,然后再返回給的客戶。
-路由:url(r'^book/(?P<id>\d+.html)',views.book),
-訪問:http://127.0.0.1:8000/book/4.html
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享題目:四、分組,反向解析、路由分發(fā)、名稱空間、偽靜態(tài)-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://jinyejixie.com/article38/cshcsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、用戶體驗、網(wǎng)站收錄、面包屑導航、網(wǎng)站營銷、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容