【问题标题】:Django Custom Include TemplateDjango 自定义包含模板
【发布时间】:2012-06-23 21:02:17
【问题描述】:

我知道这并不完全是 Django 模板理念,但我希望能够根据我在 http 响应上下文中指定的插件列表包含不同的模板。例如,如果我配置了以下插件:

 context['plugins'] = ['weather']

我尝试在基本模板文件中包含每个模板:

{% for custom_plugin in custom_plugins %}
    {% include "map/plugins/custom/{{ plugin }}/includes.html" %}
{% endfor %}

我也试过了:

{% for plugin in plugins %}
    @register.inclusion_tag("map/plugins/custom/{{ plugin }}/includes.html", takes_context=True)
{% endfor %}

目前,每个插件将仅在其 include.html 文件中包含脚本引用和 css 类:

<script type="text/javascript" src="{{ MEDIA_URL }}map/js/plugins/custom/weather/weatherStation.js?ver={{ version }}"></script>

有什么建议吗?

【问题讨论】:

  • 使用标签或变量语法inside标签是从不正确的。

标签: python django templates include


【解决方案1】:

您的第一种方法似乎是最好的,这个答案可能会为您提供一些指导:How to concatenate strings in django templates?

您基本上想要构建一个模板字符串以包含在带有 with 标记的变量中,然后包含它。

【讨论】:

  • 感谢!简单的解决方案,不知道我是如何忽略这一点的。谢谢本。
猜你喜欢
  • 2011-08-10
  • 1970-01-01
  • 2015-11-06
  • 2013-09-29
  • 2017-02-20
  • 2012-03-05
  • 1970-01-01
  • 2019-04-03
  • 2011-01-23
相关资源
最近更新 更多