在django模板中實現(xiàn)超鏈接配置
django中的超鏈接,在template中可以用{% url 'app_name:url_name' param%}
其中app_name在工程urls中配置的namespace取值,url_name是在tweb/urls.py中配置的name對應(yīng) 啟用的param參數(shù)為可選項,當函數(shù)存在的時候帶上參數(shù)對應(yīng)的取值
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^tweb/',include('tweb.urls',namespace= 'tweb')),
]
tweb/urls.py
urlpatterns = [ url(r'^index/',views.index), url(r'^addUser/',views.add_user), url(r'^show_index/',views.user), url(r'^user_page/(?P<ids>[0-9]+)$',views.user_page,name='user_page'), #ids匹配函數(shù)的參數(shù) 這樣保證每個url都是可匹配到的 ]
以上app_name對應(yīng)的就是namespace url_name的取值,,tweb/urls.py中的name對應(yīng)的是url_name
具體代碼如下
def index(request):
# user=models.user_info.objects.get(id=2) #通過id查找 也可以通過主鍵pk=1查找 結(jié)果一樣
user=models.user_info.objects.all()
return render(request,'index.html',{'values':user})
def user_page(request,ids):
user_info = models.user_info.objects.get(id=ids)
return render(request,'user_page.html',{'user_info':user_info})
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello,word</h1>
{% for value in values%}
<a href="{% url 'tweb1:user_page' value.id %}" rel="external nofollow" >{{ value.user }}</a>
{{ value.email}}
<br>
{% endfor %}
</body>
</html>
user_page.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>user page</title>
</head>
<body>
<h1>{{ user_info.user}}</h1>
<br>
<a>{{ user_info.email}}</a>
<br>
<a>{{ user_info.describe}}</a>
</body>
</html>
以上這篇在django模板中實現(xiàn)超鏈接配置就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
tensorflow 環(huán)境變量設(shè)置方式
今天小編就為大家分享一篇tensorflow 環(huán)境變量設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python中@property和property函數(shù)常見使用方法示例
這篇文章主要介紹了python中@property和property函數(shù)常見使用方法,結(jié)合實例形式分析了Python @property和property函數(shù)功能、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下2019-10-10
Python如何提取csv數(shù)據(jù)并篩選指定條件數(shù)據(jù)詳解
在學(xué)習(xí)python過程中常遇到一種情況,要讀取.csv文件的數(shù)據(jù),然后取出其中某個字段,下面這篇文章主要給大家介紹了關(guān)于Python如何提取csv數(shù)據(jù)并篩選指定條件數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-08-08

