Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django nested if else in templates

Tags:

python

django

I'm using django embeded video so when user puts youtube link video I can have the thumbnail of the video using

<img src="{{ my_video.thumbnail }}" class="img-rounded" alt="☺" height="75" width="75"/>

but when user inserts the link that that's not an youtube video I want to insert default image.

currently this is what I have

{% if post.main_image %} //if post has main_image
<img src="{{post.get_image_url}}"  class="img-rounded" alt="☺" height="75" width="75"/>
  {% elif post.url %} //if post has url
    {% video post.video as my_video %} 
      {% if my_video %}//if that url is an link to video
        <img src="{{ my_video.thumbnail }}" class="img-rounded" alt="☺" height="75" width="75"/>
        {% elif %} //if that url isn't a video
        <img src="{{post.image}}"  class="img-rounded" alt="☺ EBAGU" height="75" width="75"/>
        {% endif %}
   {% endvideo %}
{% else %} //if it  doesn't have main_image or link
<img src="{{post.thumbnail}}"  class="img-rounded" alt="☺" height="75" width="75"/>
{% endif %}

with the above code I get

TemplateSyntaxError at /post/aa-2/
Unexpected end of expression in if tag.

at {% if my_video%}

can someone please help me

This is a link to embed video app-> http://django-embed-video.readthedocs.org/en/v1.1.0/examples.html#template-examples

like image 837
mike braa Avatar asked Apr 10 '16 11:04

mike braa


1 Answers

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>
like image 118
JOSEFtw Avatar answered Sep 21 '22 18:09

JOSEFtw