【问题标题】:django template replace url by template vardjango模板用模板var替换url
【发布时间】:2016-03-29 08:56:38
【问题描述】:

是否可以动态替换模板中的 url 调用?

表格中我插入:

{% include "mytemplate.html" with soggetto='gigi' %}

模板中

  url: '{% url 'go_to_this_{{soggetto}}' %}'

有没有办法以动态方式替换 {{soggetto}} 部分,或者我应该在 url 中添加一个参数?

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    您可以使用add 模板过滤器:

    {% url 'go_to_this_'|add:soggetto %}
    

    但是,在 url 中包含 soggetto 参数可能会更简洁,然后您可以这样做:

    {% url 'go_to_this' soggetto %}
    

    【讨论】:

      猜你喜欢
      • 2018-03-01
      • 2021-08-31
      • 2013-05-04
      • 2010-12-19
      • 2014-02-24
      • 1970-01-01
      • 2018-03-01
      • 2019-07-31
      • 2011-12-22
      相关资源
      最近更新 更多