溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

四、分組,反向解析、路由分發(fā)、名稱(chēng)空間、偽靜態(tài)

發(fā)布時(shí)間:2020-08-23 18:34:45 來(lái)源:網(wǎng)絡(luò) 閱讀:258 作者:a120518129 欄目:編程語(yǔ)言

一、位置分組關(guān)鍵字分組

位置分組

  • -按位置傳參

  • -分組之后,會(huì)把分組出來(lái)的數(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')

# 訪問(wèn):http://127.0.0.1:8000/publish/

This is get


urls.py

# 匹配publish 后四位數(shù)字

url(r'^publish/[0-9]{4}/$', views.publish)

# 匹配任意長(zhǎng)度的數(shù)字

url(r'^publish/\d+/$',views.publish)

views.py

同上面一樣


urls.py

# 配后面的publish/數(shù)字4位/與數(shù)字2位 ,其他均為404 (:多個(gè)分組中都帶有小括號(hào))

url(r'^publish/([0-9]{4})/([0-9]{2})/$',views.publish),

views.py

def publish(request,year,month):    # 有括號(hào)(分組)的必須要傳參數(shù)過(guò)去
    if request.method=='GET':
        return HttpResponse(' This is get')
    elif request.method=='POST':
        return HttpResponse('This is POST')

視圖函數(shù)還可以這樣寫(xiě)(*args):

def publish(request,*args):
    if request.method == 'GET':
        return HttpResponse(' This is get')
    elif request.method == 'POST':
        return HttpResponse('This is POST')

訪問(wèn):http://127.0.0.1:8000/publish/1234/20/

This is get


urls.py

# 有括號(hào)(分組)的必須要傳參數(shù)過(guò)去,publish后面匹配多個(gè)數(shù)字

url(r'^publish/(\d+)/$',views.publish),

views.py

def publish(request,year):
    return HttpResponse("publish")

訪問(wèn):http://127.0.0.1:8000/publish/任意數(shù)字



關(guān)鍵字分組:

  • -按關(guān)鍵字傳參

  • -有名分組之后,會(huì)把分組出來(lái)的數(shù)據(jù),當(dāng)關(guān)鍵字參數(shù),傳到視圖函數(shù),所以,視圖函數(shù)需要定義形參,形參名字要跟分組的名字對(duì)應(yīng),與順序無(wú)關(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):    # 有括號(hào)(分組)的必須要傳參數(shù)過(guò)去
    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)我訪問(wèn)http://127.0.0.1:8000/publish/ 去點(diǎn)擊某一個(gè)鏈接的時(shí)候,去訪問(wèn)到了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ā)生改變的話(huà),后面可以加上‘name=自已定義的名字與pub.html網(wǎng)頁(yè)標(biāo)簽中定義的名字一致’,這樣,publishadd這個(gè)路由再如何變化,訪問(wèn)到/publish/中指定某一個(gè)鏈接時(shí),也還是會(huì)去到你已改變的那個(gè)路由上的。(類(lèi)似給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>


對(duì)于redirect 重定向的解決方案:

沒(méi)改前:

# 當(dāng)我訪問(wèn):http://127.0.0.1:8000/publish,它會(huì)幫我跳轉(zhuǎn)到http://127.0.0.1:8000/publishadd/這個(gè)頁(yè)面中,但我路由層的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ì)訪問(wèn)到views.py文件中的publishadd這個(gè)函數(shù)屬性中

例如:訪問(wèn)http://127.0.0.1:8000/publish,路由層publishadd改成了publishadd777,它也會(huì)訪問(wèn)到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ù)的話(huà),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ù)的話(huà),這里也要跟著加上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ù)的話(huà) 2018的后面還需要多傳一個(gè)參數(shù) #}
</body>
</html>

訪問(wèn):http://127.0.0.1:8000/publish/

點(diǎn)擊頁(yè)面中的 "點(diǎn)我去publishadd"

訪問(wèn)到: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>

訪問(wèn):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>

訪問(wèn):http://127.0.0.1:8000/publish/

點(diǎn)擊 :點(diǎn)我去publishadd

訪問(wèn)到: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')

訪問(wèn):http://127.0.0.1:8000/publish

會(huì)重定向到:http://127.0.0.1:8000/publishadd/2018/12/

顯示 : publishadd !~~~成功


總結(jié):

-先命一個(gè)名:
	-1 無(wú)參數(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 無(wú)參數(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 無(wú)參數(shù):url=reverse('ddd')
	2 位置分組:url=reverse('ddd',args=(2018,12,)) 如果只有一個(gè)參數(shù),后面必須要加一個(gè)逗號(hào)
	3 關(guān)鍵字分組:url=reverse('ddd',args=(2018,12,)) 還可以 url=reverse('ddd',kwargs={'year':2018,'mounth':12})


三、路由分發(fā)

作用:由總路由urls.py中先分發(fā),然后再到不同的app中由它們?cè)俅畏职l(fā)。

1、首先要再次手動(dòng)創(chuàng)建一個(gè)app

命令:python3 manage.py startapp appname (我這里就叫blog了,另一個(gè)為app01)

2、在settings.py中注冊(cè)

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、訪問(wèn):

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é)束符$

四、名稱(chēng)空間

作用:以防止兩個(gè)應(yīng)用的子路由后面的name='名字'相同或者怕沖突,這樣是為了讓它們自已找到屬于自已的名稱(chēng)空間,以防沖突(或者不要把子路由后面的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)是相對(duì)真實(shí)靜態(tài)來(lái)講的,使用*.html的話(huà),搜索引擎會(huì)認(rèn)為這些頁(yè)面不太會(huì)經(jīng)常改動(dòng),被收錄的可能性就會(huì)大,而搜索關(guān)鍵字的時(shí)候,就會(huì)把你的頁(yè)面通過(guò)搜索引擎搜索出來(lái),而其實(shí)這些頁(yè)面都是通過(guò)數(shù)據(jù)庫(kù)中查找出來(lái)的頁(yè)面,然后再返回給的客戶(hù)。

-路由:url(r'^book/(?P<id>\d+.html)',views.book),

-訪問(wèn):http://127.0.0.1:8000/book/4.html














向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI