Django urls.py重構(gòu)及參數(shù)傳遞詳解
1. 內(nèi)部重構(gòu)#

2. 外部重構(gòu)#
website/blog/urls.py

website/website/urls.py

3. 兩種參數(shù)處理方式 #
1. blog/index/?id=1234&name=bikmin#
#urls.py
url(r'^blog/index/$','get_id_name')
#views.py
from django.http import HttpResponse
from django.template import loader,Context
def get_id_name(request):
html = loader.get_template("index.html")
id = request.GET.get("id")
name = request.GET.get("name")
context = Context({"id":id,"name":name})
return HttpResponse(html.render(context))
#index.html
<body>
<li>id:{{ id }}</li>
<li>name:{{ name }}</li>
</body>
效果如下

2. blog/index/1234/bikmin#
#urls.py
url(r'^blog/index/(\d{4})/(\w+)/$','blog.views.get_id_name')
#views.py
from django.http import HttpResponse
from django.template import loader,Context
def get_id_name(request,p1,p2):
html = loader.get_template("index.html")
context = Context({"id":p1,"name":p2})
return HttpResponse(html.render(context))
#index.html
<body>
<li>id:{{ id }}</li>
<li>name:{{ name }}</li>
</body>
效果如下:

3.blog/index/1234/bikmin (和-2不一樣的在于views.py,接收的參數(shù)名是限定的)#
#urls.py
#限定id,name參數(shù)名
url(r'blog/index/(?P<id>\d{4})/(?P<name>\w+)/$','get_id_name')
#views.py
from django.http import HttpResponse
from django.template import loader,Context
def get_id_name(request,id,name):
html = loader.get_template("index.html")
context = Context({"id":id,"name":name})
return HttpResponse(html.render(context))
#index.html
<body>
<li>id:{{ id }}</li>
<li>name:{{ name }}</li>
</body>
效果如下

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python處理KNN分類算法的實(shí)現(xiàn)代碼
KNN分類算法(K-Nearest-Neighbors?Classification),又叫K近鄰算法,是一個(gè)概念極其簡單,而分類效果又很優(yōu)秀的分類算法,這篇文章主要介紹了使用Python處理KNN分類算法,需要的朋友可以參考下2022-09-09
Python字符串的15個(gè)基本操作(小結(jié))
這篇文章主要介紹了Python字符串的15個(gè)基本操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
最新PyCharm從安裝到PyCharm永久激活再到PyCharm官方中文漢化詳細(xì)教程
這篇文章涵蓋了最新版PyCharm安裝教程,最新版PyCharm永久激活碼教程,PyCharm官方中文(漢化)版安裝教程圖文并茂非常詳細(xì),需要的朋友可以參考下2020-11-11
Python經(jīng)緯度坐標(biāo)轉(zhuǎn)換為距離及角度的實(shí)現(xiàn)
這篇文章主要介紹了Python經(jīng)緯度坐標(biāo)轉(zhuǎn)換為距離及角度的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python使用get_text()方法從大段html中提取文本的實(shí)例
今天小編就為大家分享一篇Python使用get_text()方法從大段html中提取文本的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

