trn*_*snt 7 django django-templates
我想知道如何在django模板中传递url作为include的参数.
我的代码中有一个HTML文件,用于创建按钮.因此,当想要添加此按钮时,我只需包含它.问题是其中一个参数是URL,现在我还没有找到其他解决方案而不是文本URL.
所以我有这个:
{% include "button.html" with title="title" link="/new-event/" %}
Run Code Online (Sandbox Code Playgroud)
我必须有类似的东西:
{% include "button.html" with title="title" link={% url myview.view%} %}
Run Code Online (Sandbox Code Playgroud)
非常感谢你!
小智 12
你试过这个语法吗?
{% url 'some-url-name' arg arg2 as the_url %}
{% include "button.html" with title="title" link=the_url %}
Run Code Online (Sandbox Code Playgroud)
我相信您必须将url分配给添加到上下文的变量,以便在include标记中使用它.
例:
视图:
from django.core.urlresolvers import reverse
def your_view(request):
url = reverse('the_url_name_to_reverse', args=[], kwargs={})
return render(request, 'the-template.html', {'url': url})
Run Code Online (Sandbox Code Playgroud)
模板:
{% include "button.html" with title="title" link=url %}
Run Code Online (Sandbox Code Playgroud)
如果它是每个模板中需要的值,您可以考虑添加上下文处理器以将反转的url值添加到上下文中
| 归档时间: |
|
| 查看次数: |
4719 次 |
| 最近记录: |