mike braa
mike braa

Reputation: 647

django template language if and else statement, if works but else doesn't show anything

here's what I'm trying to do. User can insert url, and if that url is a youtube link I want to display the video thumbnail.(I'm using django-embeded-video) if the url isn't youtube link, I want some ohter image(post.image) to show up. So I'm using else,if statement for this.

 {% 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 %}

with above code, video thumbnail shows up when url is youtube link. but when url isn't youtube link nothing shows up.

If I do this;

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

the image shows up...

not sure why "else" isn't working

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

Upvotes: 4

Views: 1144

Answers (1)

user5243788
user5243788

Reputation:

You can Try this:

 {% 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 %}

At first You need upload post.image

Upvotes: 1

Related Questions