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

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

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

问题

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

  1. {% for article in article_roll %}
  2. <li><div class="blog-post" id="blog{{ forloop.counter }}">
  3. {% load static %}
  4. <img src="{% static '{{ article.image }}' %}" alt="{{ article.alt }}">
  5. <div class="blog-post-preview">
  6. <span class="blog-title">{{ article.image }} {{ article.title }}</span>
  7. <span class="blog-date">{{ article.date }}</span>
  8. </div>
  9. <span class="blog-text">{{ article.preview }}</span>
  10. </div></li>
  11. {% endfor %}

这部分让我感到困惑:

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

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

静态URL显示为:

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

而我想要的是:

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

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

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

英文:

Trying to display an image on a webpage with django templates

  1. {% for article in article_roll %}
  2. &lt;li&gt;&lt;div class=&quot;blog-post&quot; id=&quot;blog{{ forloop.counter }}&quot;&gt;
  3. {% load static %}
  4. &lt;img src=&quot;{% static &#39;{{ article.image }}&#39; %}&quot; alt=&quot;{{ article.alt }}&quot;&gt;
  5. &lt;div class=&quot;blog-post-preview&quot;&gt;
  6. &lt;span class=&quot;blog-title&quot;&gt;{{ article.image }} {{ article.title }}&lt;/span&gt;
  7. &lt;span class=&quot;blog-date&quot;&gt;{{ article.date }}&lt;/span&gt;
  8. &lt;/div&gt;
  9. &lt;span class=&quot;blog-text&quot;&gt;{{ article.preview }}&lt;/span&gt;
  10. &lt;/div&gt;&lt;/li&gt;
  11. {% endfor %}

This is the part that's giving me trouble

  1. &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

  1. &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

  1. &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

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

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

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

  1. &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:

确定