首页 > 解决方案 > django {%url%} 用于 {%include%}

问题描述

当我尝试渲染此模板时:

{% include "mylistitem.html" with text="foo" btn_text="bar" href="{% url 'register' %}" %}

它会产生以下错误:

TemplateSyntaxError: 无法解析余数: '"{%' from '"{%'

在文档中,我发现无法嵌套表达式。我怎么能做到这一点?

标签: djangodjango-templates

解决方案


我不相信你可以嵌套模板标签。

您当前问题的解决方案是这样做:

{% url 'register' as register_url %}
{% include "mylistitem.html" with text="foo" btn_text="bar" href=register_url  %}

这是一个重复的问题:Nested Django tags


推荐阅读