问题
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?
This is my troublesome part of form.html template:
<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>
and formset_inline.html:
<a class="btn btn-s btn-danger" href="{{ delete_url }}">
<i class="fa fa-trash-o" aria-hidden="true"></i>
</a>
When I look at the url in my browser it is empty (I have <a class="btn btn-s btn-danger" href>).
How can I pass the url?
EDIT clarification for topic added.
回答1:
I would suggest writing inclusion tag 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}
And then
<div class="col-md-12">
{% url 'accountant:gp_taxes:delete_rate' pk=field.value as delete_url %}
{% formset_inline delete_url %}
</div>
来源:https://stackoverflow.com/questions/44626788/use-variable-in-statement-in-template