一、位置分組與關(guān)鍵字分組
創(chuàng)新互聯(lián)是一家專業(yè)提供赤峰企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、HTML5建站、小程序制作等業(yè)務(wù)。10年已為赤峰眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
位置分組:
-按位置傳參
-分組之后,會(huì)把分組出來的數(shù)據(jù),當(dāng)位置參數(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 (注:多個(gè)分組中都帶有小括號)
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后面匹配多個(gè)數(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)鍵字傳參
-有名分組之后,會(huì)把分組出來的數(shù)據(jù),當(dāng)關(guān)鍵字參數(shù),傳到視圖函數(shù),所以,視圖函數(shù)需要定義形參,形參名字要跟分組的名字對應(yīng),與順序無關(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')
二、反向解析
# 分組
作用:
例如:當(dāng)我訪問http://127.0.0.1:8000/publish/ 去點(diǎn)擊某一個(gè)鏈接的時(shí)候,去訪問到了http://127.0.0.1:8000/publishadd/
urls.py
url(r'^publish/$',views.publish), url(r'^publishadd/$',views.publishadd,name='pub'),
# 若以后要去到這個(gè)地址(publishadd)會(huì)發(fā)生改變的話,后面可以加上‘name=自已定義的名字與pub.html網(wǎng)頁標(biāo)簽中定義的名字一致’,這樣,publishadd這個(gè)路由再如何變化,訪問到/publish/中指定某一個(gè)鏈接時(shí),也還是會(huì)去到你已改變的那個(gè)路由上的。(類似給publishadd,取了一個(gè)別名)
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'%}">點(diǎn)我去publishadd</a> </body> </html>
對于redirect 重定向的解決方案:
沒改前:
# 當(dāng)我訪問:http://127.0.0.1:8000/publish,它會(huì)幫我跳轉(zhuǎn)到http://127.0.0.1:8000/publishadd/這個(gè)頁面中,但我路由層的publishadd換了其他名字的時(shí)候,便會(huì)報(bào)錯(cuò)了。
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'%}">點(diǎn)我去publishadd</a> {#<a href="{% url 'pub' year=2018 month=12 %}">test去點(diǎn)我</a>#} </body> </html>
改之后:路由中的publishadd后面再如何改也會(huì)訪問到views.py文件中的publishadd這個(gè)函數(shù)屬性中
例如:訪問http://127.0.0.1:8000/publish,路由層publishadd改成了publishadd777,它也會(huì)訪問到publishadd這個(gè)函數(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后面是兩個(gè)參數(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個(gè)參數(shù)的話,這里也要跟著加上2個(gè)參數(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 %}">點(diǎn)我去publishadd</a> {# 如果上面是兩個(gè)參數(shù)的話 2018的后面還需要多傳一個(gè)參數(shù) #} </body> </html>
訪問:http://127.0.0.1:8000/publish/
點(diǎn)擊頁面中的 "點(diǎn)我去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 %}">點(diǎn)我去publishadd</a> </body> </html>
訪問:http://127.0.0.1:8000/publish
自動(dòng)跳轉(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 %}">點(diǎn)我去publishadd</a> </body> </html>
訪問:http://127.0.0.1:8000/publish/
點(diǎn)擊 :點(diǎn)我去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
會(huì)重定向到:http://127.0.0.1:8000/publishadd/2018/12/
顯示 : publishadd !~~~成功
總結(jié):
-先命一個(gè)名: -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,)) 如果只有一個(gè)參數(shù),后面必須要加一個(gè)逗號 3 關(guān)鍵字分組:url=reverse('ddd',args=(2018,12,)) 還可以 url=reverse('ddd',kwargs={'year':2018,'mounth':12})
三、路由分發(fā)
作用:由總路由urls.py中先分發(fā),然后再到不同的app中由它們再次分發(fā)。
1、首先要再次手動(dòng)創(chuàng)建一個(gè)app
命令:python3 manage.py startapp appname (我這里就叫blog了,另一個(gè)為app01)
2、在settings.py中注冊
INSTALLED_APPS 的列表中添加
'blog.apps.BlogConfig',
3、在每個(gè)app的目錄下面創(chuàng)建一個(gè)名為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)系 ***重點(diǎn)***總路由,不能加結(jié)束符$
四、名稱空間
作用:以防止兩個(gè)應(yīng)用的子路由后面的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)是相對真實(shí)靜態(tài)來講的,使用*.html的話,搜索引擎會(huì)認(rèn)為這些頁面不太會(huì)經(jīng)常改動(dòng),被收錄的可能性就會(huì)大,而搜索關(guān)鍵字的時(shí)候,就會(huì)把你的頁面通過搜索引擎搜索出來,而其實(shí)這些頁面都是通過數(shù)據(jù)庫中查找出來的頁面,然后再返回給的客戶。
-路由:url(r'^book/(?P<id>\d+.html)',views.book),
-訪問:http://127.0.0.1:8000/book/4.html
網(wǎng)站名稱:四、分組,反向解析、路由分發(fā)、名稱空間、偽靜態(tài)
分享鏈接:http://muchs.cn/article30/piodpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、虛擬主機(jī)、ChatGPT、面包屑導(dǎo)航、靜態(tài)網(wǎng)站、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)