Python Django在模板中嵌套if else
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36528958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django nested if else in templates
提问by mike braa
I'm using django embeded video so when user puts youtube link video I can have the thumbnail of the video using
我正在使用 django 嵌入视频,所以当用户放置 youtube 链接视频时,我可以使用视频的缩略图
<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.
但是当用户插入不是 YouTube 视频的链接时,我想插入默认图像。
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%}
{% 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
这是嵌入视频应用程序的链接-> http://django-embed-video.readthedocs.org/en/v1.1.0/examples.html#template-examples
回答by JOSEFtw
Your {% elif %}
in {% if my video...%}
doesn't have any condition.
你的{% elif %}
in{% if my video...%}
没有任何条件。
I think you should have {% else %}
instead?
我想你应该有 {% else %}
吗?
{% 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:
基于评论中的 dpaste 的固定版本:
<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>