需要帮助。我正在尝试使for语句中的if语句正确,但失败了。如果没有可用的图像,我非常需要备用图像来替换它。
谢谢
{% for post in posts|slice:":1" %}
<div class="container">
{% if post.image.url %}
<div class="flex-item">
<a href="{{ post.get_absolute_url }}"><img style="width:100%;" src="{{ post.image.url }}"</a>
</div>
{% else %}
<div class="image-container1">
<img src="{% static 'images/fallback.png' %}" alt="" style="width: 100%;text-align: center;display: block;">
</div>
{% endif %}
{% endfor %}
您没有关闭在第一个循环之后打开的div,并且假设您在顶部加载了static。
P.S。我无法添加评论,因此将其发布为ans。