Python Django自定义包含模板

Python Django自定义包含模板,python,django,templates,include,Python,Django,Templates,Include,我知道这并不完全是Django的模板哲学,但我希望能够根据我在http响应上下文中指定的插件列表包含不同的模板。例如,如果我配置了以下插件: context['plugins'] = ['weather'] 我尝试在基本模板文件中包含每个模板: {% for custom_plugin in custom_plugins %} {% include "map/plugins/custom/{{ plugin }}/includes.html" %} {% endfor %} 我也试

我知道这并不完全是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>


有什么建议吗?

您的第一种方法似乎是最好的,这个答案可能会为您提供一些建议:


基本上,您希望构建一个模板字符串,将其包含在带有with标记的变量中,然后将其包含在内

格雷西亚斯!简单的解决方案,不确定我是如何忽略了这一点。谢谢Ben。在标记中使用标记或变量语法是不正确的。