使用 {{ url() }} 的 Django 和 Jinja2 模板

标签 django jinja2

我正在尝试弄清楚如何使用 jinja 的 {{ url() }} 在我的 html 中传递我的 user_id,使用不需要任何 id 的 url,比如 /dashboard/ 工作正常但是我需要将一个 id 传递给这个示例:/user/3。我尝试了以下但没有成功:

{{ url('detail') }}
{{ url('detail', user_id=User.id) }}
{{ url('detail', User.id) }}

这是我的部分观点和html:

View .py

urlpatterns = [
    path('dashboard/', dashboard, name='dashboard'),          
    path('user/<int:user_id>/', detail, name='detail'),    
]

仪表板.html

{% for User in all_users %}

    {{ url('detail') }}

{% endfor %}

如有任何帮助,我们将不胜感激,谢谢

最佳答案

我找到了一个解决方案:

{% for User in all_users %}
    {{ url('detail', args=[User.id] )}}
{% endfor %}

关于使用 {{ url() }} 的 Django 和 Jinja2 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48695605/

相关文章:

templates - Ansible 取消保管和模板化文件

python - Django select_related : How to display related table data in a template

python - selenium(使用 python/django)无法启动 firefox 浏览器实例

python - Django 1.7 - 我如何抑制 "(1_6.W001) Some project unittests may not execute as expected."?

python - 使用 Flask 生成页面而不使用单独的模板文件

ansible - 检查 Ansible 中的字符串是否等于和三元运算符

python - pinax 有哪些替代品?

python - SQLAlchemy 是否与 Django 的 get_or_create 等效?

python - 在Google App Engine中,如何使用python通过 bool 值进行查询?

python - NameError:全局名称 'TemplateNotFound' 未定义