Use variable in statement in template

本秂侑毒 提交于 2019-12-11 04:39:30

问题


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>&nbsp;
</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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!