Python测试开发django5.urls.py参数name与a标签的引用
上一篇,我們學(xué)習(xí)了Python測試開發(fā)django5.templates模板變量傳參
? ? ? ?如果我們有2個頁面home.html和demo.html,兩個頁面是獨立的沒有關(guān)系的,現(xiàn)在需要從home.html頁,點某個超鏈按鈕,跳轉(zhuǎn)到demo.html頁,home.html頁面就需要配置demo.html的頁面鏈接
項目目錄
helloworld\hello\templates\demo.html文件寫入內(nèi)容
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>demo模板</title> </head> <body><p><h4> 我的博客 </h4><a href="https://blog.csdn.net/qq_36502272" target="_blank" > 博客網(wǎng)址 </a><hr><h4> 軟件測試技術(shù)交流分享 </h4><p>軟件測試技術(shù)、方法、測試方案分享交流、python自動化測試、性能Jmeter工具交流學(xué)習(xí)<br>QQ交流群212683165</p><a href="https://blog.csdn.net/qq_36502272" target="_blank" >點擊訪問博客</a> </p></body> </html>helloworld\hello\templates\home.html文件寫入內(nèi)容
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>home模板</title> </head> <body><p> 歡迎進入home頁面<hr><a href="demo" > 點擊跳轉(zhuǎn)demo頁面 </a> </p></body> </html>helloworld\hello\views.py文件寫入內(nèi)容
from django.shortcuts import render from django.http import HttpResponse # Create your views here.# def home(request): # return HttpResponse("hello") def home(request):return render(request, 'home.html')def demo(request):return render(request, 'demo.html')helloworld\helloworld\urls.py文件寫入內(nèi)容
from django.contrib import admin from django.urls import path from django.conf.urls import url from hello import viewsurlpatterns = [path('admin/', admin.site.urls),url('^demo$', views.demo),url('^home$', views.home), ]這樣就實現(xiàn)了從home.html頁,點擊某個超鏈接跳轉(zhuǎn)demo.html頁
我們目前是把<a href="demo" > 點擊跳轉(zhuǎn)demo頁面 </a>在home.html頁寫四的,如果多個頁面都用到這個鏈接時,地址發(fā)生變化了,修改的地方就太多了,維護成本太高,這時候我們就可以給url標(biāo)記上唯一的名字,也就是name參數(shù),接下來在url配置里加個name名稱,由原來的url('^demo$', views.demo)變?yōu)閡rl('^demo$', views.demo,name="Demo")
helloworld\helloworld\urls.py文件更新為:
urlpatterns = [path('admin/', admin.site.urls),url('^demo$', views.demo,name="Demo"),url('^home$', views.home), ]對于像<a>這樣鏈接的操作用法:?
不帶參數(shù)的:{% url 'name' %}
帶參數(shù)的:參數(shù)可以是變量名?{% url 'name' 參數(shù) %}?
home.html里<a href="demo" > 點擊跳轉(zhuǎn)demo頁面 </a>變更為<a href={% url 'Demo' %} > 點擊跳轉(zhuǎn)demo頁面 </a>
helloworld\hello\templates\home.html文件更新為:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>home模板</title> </head> <body><p> 歡迎進入home頁面<hr><a href={% url 'Demo' %} > 點擊跳轉(zhuǎn)demo頁面 </a> </p></body> </html>所以這里的改變?yōu)?#xff1a;url('^demo$', views.demo,name="Demo")(對于其他部分自動補腦),在<a href={% url 'Demo' %} > 點擊跳轉(zhuǎn)demo頁面 </a>? ? 解釋:{% url 'Demo' %}這里的Demo'代表著這個url自動去尋找django的url里面name="Demo'"這條鏈接,并將找到的url -demo.html賦值給<a>標(biāo)簽中的href,當(dāng)鏈接發(fā)生變化,我們只需要更新urls.py里面的'^demo$'就可以了。
【UI設(shè)計、平面設(shè)計、LOGO設(shè)計、淘寶設(shè)計需求】商務(wù)合作QQ:2017340535
【軟件測試方案設(shè)計、測試方法指導(dǎo)】商務(wù)合作QQ:2017340535
總結(jié)
以上是生活随笔為你收集整理的Python测试开发django5.urls.py参数name与a标签的引用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fiddler抓包工具之Filters(
- 下一篇: 测试人员做到这几点,线上80%的BUG将