[Fixed]-Django nested if else in templates

27👍

Your {% elif %} in {% if my video...%} doesn’t have any condition.

I think you should have {% else %} instead?

{% if my_video %}//if that url is an link to video
    <img src="{{ my_video.thumbnail }}" class="img-rounded" alt="" height="75" width="75"/>
{% else %} //if that url isn't a video
    <img src="{{post.image}}"  class="img-rounded" alt=" EBAGU" height="75" width="75"/>
{% endif %}

Fixed version based on the dpaste in the comments:

<td>
    {% if post.main_image %}
        <img src="{{post.get_image_url}}"  class="img-rounded" alt="☺" height="75" width="75"/>
    {% elif 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 %}
        <img src="{{post.thumbnail}}"  class="img-rounded" alt="☺" height="75" width="75"/>
    {% endif %}
</td>

Leave a comment