Syntax for using {{ article.image }} inside {% static %} with django templates

huangapple go评论56阅读模式
英文:

Syntax for using {{ article.image }} inside {% static %} with django templates

问题

尝试在使用Django模板的网页上显示图像。

{% for article in article_roll %}
    <li><div class="blog-post" id="blog{{ forloop.counter }}">
        {% load static %}
        <img src="{% static '{{ article.image }}' %}" alt="{{ article.alt }}">
        <div class="blog-post-preview">
            <span class="blog-title">{{ article.image }} {{ article.title }}</span>
            <span class="blog-date">{{ article.date }}</span>
        </div>
        <span class="blog-text">{{ article.preview }}</span>
    </div></li>
{% endfor %}

这部分让我感到困惑:

<img src="{% static '{{ article.image }}' %}" alt="{{ article.alt }}">

{{ article.image }} 是SQLite数据库中的一个ImageField,使用了Django的默认配置。我主要关注的是在for循环进行时加载正确的图像,但是我甚至无法让{{ article.image }} 在 {% static %} 中得到有用的值。

静态URL显示为:

<img src="/static/%7B%7B%20article.image%20%7D%7D" alt="image thing">

而我想要的是:

<img src="/static/value_of_{{article.image}}" alt="image thing">

我尝试过转义字符、避免使用' ',以及重写URL,但都没有成功。

我觉得我可能完全错误地解决了这个问题,可能有更好的方法,但我已经查阅了文档,没有找到明显的解决方案。

英文:

Trying to display an image on a webpage with django templates

{% for article in article_roll %}
    &lt;li&gt;&lt;div class=&quot;blog-post&quot; id=&quot;blog{{ forloop.counter }}&quot;&gt;
        {% load static %}
        &lt;img src=&quot;{% static &#39;{{ article.image }}&#39; %}&quot; alt=&quot;{{ article.alt }}&quot;&gt;
        &lt;div class=&quot;blog-post-preview&quot;&gt;
            &lt;span class=&quot;blog-title&quot;&gt;{{ article.image }} {{ article.title }}&lt;/span&gt;
            &lt;span class=&quot;blog-date&quot;&gt;{{ article.date }}&lt;/span&gt;
        &lt;/div&gt;
        &lt;span class=&quot;blog-text&quot;&gt;{{ article.preview }}&lt;/span&gt;
    &lt;/div&gt;&lt;/li&gt;
{% endfor %}

This is the part that's giving me trouble

&lt;img src=&quot;{% static &#39;{{ article.image }}&#39; %}&quot; alt=&quot;{{ article.alt }}&quot;&gt;

{{ article.image }} is an ImageField in an SQLite Database setup with the default configurations django has. My main concern is loading up the correct image for each article as the for loop progresses, but I can't even get {{ article.image }} to evaluate to anything useful within the {% static %} braces.

the static url comes out as

&lt;img src=&quot;/static/%7B%7B%20article.image%20%7D%7D&quot; alt=&quot;image thing&quot;&gt;

When what I'm trying to get is

&lt;img src=&quot;/static/value_of_{{article.image}}&quot; alt=&quot;image thing&quot;&gt;

I've tried escaping characters, avoiding using ' ', and rewriting the urls.

I feel like I might be approaching this problem entirely wrong and there's a better way to do it, but I've been looking through the documentation and haven't seen anything obvious to me.

答案1

得分: 0

你不需要在静态标签内使用'{{ }}'

<img src="{% static article.image %}" alt="{{ article.alt }}">
英文:

You don't need the &#39;{{ }}&#39; inside the static tag.

&lt;img src=&quot;{% static article.image %}&quot; alt=&quot;{{ article.alt }}&quot;&gt;

huangapple
  • 本文由 发表于 2023年1月8日 22:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定