1、位置分組與關鍵字分組html
位置分組:python
-按位置傳參數據庫
-分組以後,會把分組出來的數據,當位置參數,傳到視圖函數,因此,視圖函數須要定義形參django
urls.py
app
# 精確匹配ide
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 geturl
urls.pyspa
# 匹配publish 後四位數字
url(r'^publish/[0-9]{4}/$', views.publish)
# 匹配任意長度的數字
url(r'^publish/\d+/$',views.publish)
views.py
同上面同樣
urls.py
# 配後面的publish/數字4位/與數字2位 ,其餘均爲404 (注:多個分組中都帶有小括號)
url(r'^publish/([0-9]{4})/([0-9]{2})/$',views.publish),
views.py
def publish(request,year,month): # 有括號(分組)的必需要傳參數過去 if request.method=='GET': return HttpResponse(' This is get') elif request.method=='POST': return HttpResponse('This is POST')
視圖函數還能夠這樣寫(*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
# 有括號(分組)的必需要傳參數過去,publish後面匹配多個數字
url(r'^publish/(\d+)/$',views.publish),
views.py
def publish(request,year): return HttpResponse("publish")
訪問:http://127.0.0.1:8000/publish/任意數字
關鍵字分組:
-按關鍵字傳參
-有名分組以後,會把分組出來的數據,當關鍵字參數,傳到視圖函數,因此,視圖函數須要定義形參,形參名字要跟分組的名字對應,與順序無關
urls.py
# 關鍵字分組是按照關鍵字傳參數
url(r'^publish/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$',views.publish),
views.py
def publish(request,year,month): # 有括號(分組)的必需要傳參數過去 if request.method=='GET': return HttpResponse(' This is get') elif request.method=='POST': return HttpResponse('This is POST')
urls.py
# 位置分組與關鍵字分組混合使用
url(r'^publish/([0-9]{4})/(?P<month>[0-9]{2})/$',views.publish),
views.py
# 位置分組與關鍵字分組混合使用,能夠使用*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')
2、反向解析
# 分組
做用:
例如:當我訪問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)會發生改變的話,後面能夠加上‘name=自已定義的名字與pub.html網頁標籤中定義的名字一致’,這樣,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,它會幫我跳轉到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這個函數屬性中
例如:訪問http://127.0.0.1:8000/publish,路由層publishadd改爲了publishadd777,它也會訪問到publishadd這個函數的屬性中
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 不變
# 位置分組,參數傳遞
urls.py
url(r'^publish/$',views.publish), url(r'^publishadd/([0-9]{4})/$',views.publishadd,name='pub'), # 若是publishadd後面是兩個參數的話,views.py中的也須要跟着加
views.py
def publish(request): if request.method=='GET': return render(request, 'pub.html') def publishadd(request,year): # urls.py 中的publishadd後面要加2個參數的話,這裏也要跟着加上2個參數,例如: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> {# 若是上面是兩個參數的話 2018的後面還須要多傳一個參數 #} </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
自動跳轉到:http://127.0.0.1:8000/publishadd/2018/12/
顯示:publishadd 成功。
# 關鍵字分組 --- 模板層
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> {#關鍵字分組#} <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 成功~
# 關鍵字視圖層
urls.py 與 templates/pub.html 內容不變(與上面的一致)
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 !~~~成功
總結:
-先命一個名: -1 無參數:url(r'^publishadd133/$', views.publishadd,name='ddd'), -2 位置分組:url(r'^publishadd/([0-9]{4})/([0-9]{2})/$', views.publishadd,name='ddd'), -3 關鍵字分組:url(r'^publishadd/(?P<year>[0-9]{4})/(?P<mounth>[0-9]{2})/$', views.publishadd,name='ddd'), -在模板層: -1 無參數:{% url 'ddd' %} -2 位置分組的:{% url 'ddd' 2018 12 %} -3 關鍵分組:{% url 'ddd' 2018 12 %} 還能夠 {% url 'ddd' year=2018 mounth=12 %} -在視圖層: from django.shortcuts import reverse 在視圖函數裏: 1 無參數:url=reverse('ddd') 2 位置分組:url=reverse('ddd',args=(2018,12,)) 若是隻有一個參數,後面必需要加一個逗號 3 關鍵字分組:url=reverse('ddd',args=(2018,12,)) 還能夠 url=reverse('ddd',kwargs={'year':2018,'mounth':12})
3、路由分發
做用:由總路由urls.py中先分發,而後再到不一樣的app中由它們再次分發。
一、首先要再次手動建立一個app
命令:python3 manage.py startapp appname (我這裏就叫blog了,另外一個爲app01)
二、在settings.py中註冊
INSTALLED_APPS 的列表中添加
'blog.apps.BlogConfig',
三、在每一個app的目錄下面建立一個名爲urls.py的文件。內容爲:
app01目錄下的urls.py中:
from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^app01_test/$',views.test), ]
app01目錄下的視圖函數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目錄下的視圖函數views.py文件中:
def blog(request): return HttpResponse('blog----test')
四、在總路由中的urls.py中的須要配置如下:
from django.conf.urls import url,include # 總路由中添加: url(r'^app01/',include('app01.urls')), url(r'^blog/',include('blog.urls')),
五、訪問:
http://127.0.0.1:8000/blog/blog_test/
http://127.0.0.1:8000/app01/app01_test/
總結:
路由分發 1 在不一樣的app裏建立urls.py 2 在總路由 -from django.conf.urls import include -url(r'^blog/',include('blog.urls')), -url(r'^app01/',include('app01.urls')), 3 在不一樣的app的urls裏配置路由關係 ***重點***總路由,不能加結束符$
4、名稱空間
做用:以防止兩個應用的子路由後面的name='名字'相同或者怕衝突,這樣是爲了讓它們自已找到屬於自已的名稱空間,以防衝突(或者不要把子路由後面的name='名字'名字命名成同樣的也可。作了解)
一、總路由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>
5、僞靜態
僞靜態是相對真實靜態來說的,使用*.html的話,搜索引擎會認爲這些頁面不太會常常改動,被收錄的可能性就會大,而搜索關鍵字的時候,就會把你的頁面經過搜索引擎搜索出來,而其實這些頁面都是經過數據庫中查找出來的頁面,而後再返回給的客戶。
-路由:url(r'^book/(?P<id>\d+.html)',views.book),
-訪問:http://127.0.0.1:8000/book/4.html