首页 > 解决方案 > 如何将 url 参数传递给模板中的 url 标签

问题描述

我正在使用 Django 3.1

我已配置 URL,以便我可以在模板中正确使用以下内容:

<li><a href="{% url 'sitemember:profile' 'bobby' %}">{{ user.username }}&nbsp;&nbsp;</a></li>

其中 user.username 解析为 'bobby'

如何将变量传递user.usernameurl()标记,以便 URL 正确解析为:

https://localhost:/user/bobby(例如)

标签: djangodjango-templates

解决方案


您在{% url … %}模板标签 [Django-doc]中指定:

<a href="{% url 'sitemember:profile' user.username %}">

推荐阅读