Django urls.py重構及參數(shù)傳遞詳解
1. 內部重構#

2. 外部重構#
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>
效果如下

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
最新PyCharm從安裝到PyCharm永久激活再到PyCharm官方中文漢化詳細教程
這篇文章涵蓋了最新版PyCharm安裝教程,最新版PyCharm永久激活碼教程,PyCharm官方中文(漢化)版安裝教程圖文并茂非常詳細,需要的朋友可以參考下2020-11-11
Python經(jīng)緯度坐標轉換為距離及角度的實現(xiàn)
這篇文章主要介紹了Python經(jīng)緯度坐標轉換為距離及角度的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
Python使用get_text()方法從大段html中提取文本的實例
今天小編就為大家分享一篇Python使用get_text()方法從大段html中提取文本的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

