Python 将静态路径传递到包含的模板

Python 将静态路径传递到包含的模板,python,django,Python,Django,是否可以将静态路径作为变量传递给包含的模板 我需要反复包括在我的网页模板,但它总是与不同的标志 例如: {% with img={% static 'img/dashboards/belgium_flag.svg' %} %} {% include 'dashboards/charts/country_block.html' %} {% endwith %} {% with img={% static 'img/dashboards/netherland

是否可以将静态路径作为变量传递给包含的模板

我需要反复包括在我的网页模板,但它总是与不同的标志

例如:

    {% with img={% static 'img/dashboards/belgium_flag.svg' %} %}
        {% include 'dashboards/charts/country_block.html' %}
    {% endwith %}

    {% with img={% static 'img/dashboards/netherlands_flag.svg' %} %}
        {% include 'dashboards/charts/country_block.html' %}
    {% endwith %}
这不管用


除了创建一个模型来支持每个国家/地区实例的图像属性外,还有其他解决方法吗?

您可以使用
{%…as…%}
子句来实现这一点:

{% static 'img/dashboards/belgium_flag.svg' as img %}
{% include 'dashboards/charts/country_block.html' %}


{% static 'img/dashboards/netherlands_flag.svg' as img %}
{% include 'dashboards/charts/country_block.html' %}
{%static'img/dashboards/belling_flag.svg'作为img%}
{%include'dashboards/charts/country_block.html%}
{%static'img/dashboards/inherlands_flag.svg'作为img%}

{%include'dashboards/charts/country_block.html%}
我不相信您可以嵌套
{%%}
标记。使用img='img/dashboards/belling_flag.svg'尝试
,然后在包含的模板内处理
{%static%}
标记。(是否存在图像路径不处于静态状态的情况?)您不需要带有
?@JohnGordon:不,我们在这里定义一个局部变量
img
,并将该值设置为该局部变量。太好了!我不知道你能做到。