在模板的语句中使用变量 [英] Use variable in statement in template

查看:70
本文介绍了在模板的语句中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于在模板的语句中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆