如何在 Django 静态标签中使用字典变量
How to use dictionary variables inside django static tag
我正在尝试将 Django 变量连接到 img src
。我找到了 solution 来执行此操作,但是当我检查浏览器时,该值仍然没有显示。顺便说一句,我调用的值是 dict
类型,看起来像这样 >>> djangoDict
输出:{<foreignModel: ModelValue>: <Product: Item Desc...}
我能够使用 2 个大括号 {{ djangoDict }}
来显示 ModelValue
,但在与地址连接时我无法做到这一点。
所以这是我的代码:
{% for dd in djangoDict %}
{% with 'products/anotherFolder/'|add:dd|add:'/suffix.jpg' as myImg %}
<a class="dropdown-item" data-img="{% static myImg %}">{{ dd }}</a>
{% endwith %}
{% endfor %
这是我在检查元素时得到的:
<a class="dropdown-item" data-img="/assets/suffix.jpg">ModelValue</a>
我想展示的内容:
<a class="dropdown-item" data-img="/assets/products/anotherFolder/ModelValue/suffix.jpg">ModelValue</a>
非常感谢任何愿意提供帮助的人:)
我看到了一个与我类似的问题,它解决了我在连接图像路径方面的问题。 It is the best answer 我的问题。
我使用了 STATIC_URL
,它是在 settings.py
上配置的
我的代码现在看起来像这样:
{% for dd in djangoDict %}
<a class="dropdown-item" data-img="{{ STATIC_URL }}products/anotherFolder/{{ dd }}/suffix.jpg">{{ dd }}</a>
{% endfor %}
我也遇到了类似的问题,不知道是不是一模一样;我想 link 我的静态文件夹中的一个文件到我的 HTML 文件,即 src=""
,目录应该来自字典源。
显然你可以直接 link 它在那里。这是我的部分代码;
{% for dic in posts %}
{% if dic.no == 1 %}
<img alt="" class="u-expanded-width u-image u-image-default src="{% static dic.src %}"><!--/product_image--><!--product_title-->
<h4 class="u-product-control u-text u-text-body-alt-color u-text-2">
<a class="u-product-title-link" href="#"><!--product_title_content-->{{ dic.name }}<!--/product_title_content--></a>
{% endif %}
{% endfor %}
我正在尝试将 Django 变量连接到 img src
。我找到了 solution 来执行此操作,但是当我检查浏览器时,该值仍然没有显示。顺便说一句,我调用的值是 dict
类型,看起来像这样 >>> djangoDict
输出:{<foreignModel: ModelValue>: <Product: Item Desc...}
我能够使用 2 个大括号 {{ djangoDict }}
来显示 ModelValue
,但在与地址连接时我无法做到这一点。
所以这是我的代码:
{% for dd in djangoDict %}
{% with 'products/anotherFolder/'|add:dd|add:'/suffix.jpg' as myImg %}
<a class="dropdown-item" data-img="{% static myImg %}">{{ dd }}</a>
{% endwith %}
{% endfor %
这是我在检查元素时得到的:
<a class="dropdown-item" data-img="/assets/suffix.jpg">ModelValue</a>
我想展示的内容:
<a class="dropdown-item" data-img="/assets/products/anotherFolder/ModelValue/suffix.jpg">ModelValue</a>
非常感谢任何愿意提供帮助的人:)
我看到了一个与我类似的问题,它解决了我在连接图像路径方面的问题。 It is the best answer 我的问题。
我使用了 STATIC_URL
,它是在 settings.py
我的代码现在看起来像这样:
{% for dd in djangoDict %}
<a class="dropdown-item" data-img="{{ STATIC_URL }}products/anotherFolder/{{ dd }}/suffix.jpg">{{ dd }}</a>
{% endfor %}
我也遇到了类似的问题,不知道是不是一模一样;我想 link 我的静态文件夹中的一个文件到我的 HTML 文件,即 src=""
,目录应该来自字典源。
显然你可以直接 link 它在那里。这是我的部分代码;
{% for dic in posts %}
{% if dic.no == 1 %}
<img alt="" class="u-expanded-width u-image u-image-default src="{% static dic.src %}"><!--/product_image--><!--product_title-->
<h4 class="u-product-control u-text u-text-body-alt-color u-text-2">
<a class="u-product-title-link" href="#"><!--product_title_content-->{{ dic.name }}<!--/product_title_content--></a>
{% endif %}
{% endfor %}