在模板的语句中使用变量
问题描述:
我想在Django模板的 {%include%}
语句中使用变量。具体来说,我试图在另一个模板中包含一个模板,并且我需要生成并传递要在按钮中使用的url。我该如何实现?
I want to use a variable in {% include %}
statement in django templates. Specifically, I am trying to include a template in another template and i need to generate and pass url to be used in a button. How can I achieve this?
这是 form.html
模板的麻烦部分:
<div class="col-md-12">
{% url 'accountant:gp_taxes:delete_rate' pk=field.value as delete_url %}
{% include 'includes/formset_inline.html' with delete_url=delete_url %}
</div>
和 formset_inline.html
:
<a class="btn btn-s btn-danger" href="{{ delete_url }}">
<i class="fa fa-trash-o" aria-hidden="true"></i>
</a>
当我在浏览器中查看网址时,它为空(我有 < a class = btn btn-s btn-danger href>
)。
When I look at the url in my browser it is empty (I have <a class="btn btn-s btn-danger" href>
).
如何传递网址?
编辑添加了对主题的澄清。
EDIT clarification for topic added.
答
我建议写包含标签
https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/#inclusion-tags
@register.inclusion_tag('includes/formset_inline.html')
def formset_inline(delete_url):
return {'delete_url': delete_url}
然后
<div class="col-md-12">
{% url 'accountant:gp_taxes:delete_rate' pk=field.value as delete_url %}
{% formset_inline delete_url %}
</div>