在django模板中实现超链接配置
django中的超链接,在template中可以用{% url 'app_name:url_name' param%}
其中app_name在工程urls中配置的namespace取值,url_name是在tweb/urls.py中配置的name对应 启用的param参数为可选项,当函数存在的时候带上参数对应的取值
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匹配函数的参数 这样保证每个url都是可匹配到的 ]
以上app_name对应的就是namespace url_name的取值,,tweb/urls.py中的name对应的是url_name
具体代码如下
def index(request): # user=models.user_info.objects.get(id=2) #通过id查找 也可以通过主键pk=1查找 结果一样 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模板中实现超链接配置就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Python Excel操作从零学习掌握openpyxl用法
这篇文章主要为大家介绍了Python Excel操作从零学习掌握openpyxl用法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-08-08python-docx把dataframe表格添加到word文件中
用Python-docx库,可以轻松地添加表格到Word文档中,本文主要介绍了python-docx把dataframe表格添加到word文件中,感兴趣的可以了解一下2023-08-08Python编程使用*解包和itertools.product()求笛卡尔积的方法
这篇文章主要介绍了Python编程使用*解包和itertools.product()求笛卡尔积的方法,涉及Python列表转换及itertools.product()求笛卡尔积相关操作技巧,需要的朋友可以参考下2017-12-12python实现word文档批量转成自定义格式的excel文档的思路及实例代码
这篇文章主要介绍了python实现word文档批量转成自定义格式的excel文档的解决思路及实例代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下2020-02-02
最新评论