python – django模板语言if和else语句,如果有效,但不显示任何内容

这就是我想要做的.用户可以插入网址,如果该网址是youtube链接,我想显示视频缩略图.(我正在使用
django-embeded-video)如果网址不是youtube链接,我想要一些图像(post.image) ) 现身.所以我正在使用其他,如果声明的话.

 {% if post.url %}
  {% video post.url as my_video %}
  {% if my_video %}
        <img src="{{ my_video.thumbnail }}" class="img-rounded" alt="☺" height="75" width="75"/>
  {% else %}
<img src="{{post.image}}"  class="img-rounded" alt="☺" height="75" width="75"/>
  {% endif %}
{% endvideo %}
{% endif %}

使用上面的代码,当url是youtube链接时,视频缩略图会显示出来.但当url不是youtube链接时,没有任何显示.

如果我这样做;

  {% if post.url %}
<img src="{{post.image}}"  class="img-rounded" alt="☺ EBAGU" height="75" width="75"/>

图像显示出来……

不知道为什么“其他”不起作用

Edit: img src={{post.image}} works 
img src={{my_video.thumbnail}} works
I want if img src={{my_video.thumbnail}} isn't there I want {{post.image}} to appear

最佳答案 你可以尝试这个:

 {% if post.url %}
   {% video post.url as my_video %}
   {% if my_video %}
       <img src="{{ my_video.thumbnail }}"/>
   {% else %}
       <img src="{{ MEDIA_URL }}{{ post.image }}"/>
       <!-- or <img src="{% static 'Path/To?You/static/image.png' %}"/> if You not upload image everytime-->
   {% endif %}
 {% endif %}

首先你需要上传post.image

点赞