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實(shí)現(xiàn)最新房價(jià)信息的獲取
這篇文章主要為大家介紹了如何利用Python獲取房價(jià)信息(以北京為例),整個(gè)數(shù)據(jù)獲取的信息是通過房源平臺獲取的,通過下載網(wǎng)頁元素并進(jìn)行數(shù)據(jù)提取分析完成整個(gè)過程,需要的可以參考一下2022-04-04Python-copy()與deepcopy()區(qū)別詳解
這篇文章主要介紹了Python-copy()與deepcopy()區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07python創(chuàng)建學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python創(chuàng)建學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Python?Flask實(shí)現(xiàn)快速構(gòu)建Web應(yīng)用的方法詳解
Flask是一個(gè)輕量級的Web服務(wù)器網(wǎng)關(guān)接口(WSGI)web應(yīng)用框架,本文將和大家一起詳細(xì)探討一下Python?Flask?Web服務(wù),需要的小伙伴可以學(xué)習(xí)一下2023-06-06Python+wxPython實(shí)現(xiàn)批量文件擴(kuò)展名替換
這篇文章主要介紹了如何使用 Python和wxPython創(chuàng)建一個(gè)簡單的圖形界面應(yīng)用程序,使用戶能夠選擇文件夾、輸入要替換的文件類型和新的文件類型,并實(shí)現(xiàn)批量替換文件擴(kuò)展名的功能,有需要的可以參考一下2023-10-10