Django自定义包含模板

时间:2012-06-23 21:02:17

标签: python django templates include

我知道这不是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 %}

现在每个插件只在其includes.html文件中包含脚本引用和css类:

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

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你的第一种方式似乎是最好的,这个答案可能会提供一些关于你如何去做的指示:How to concatenate strings in django templates?

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

相关问题