【问题标题】:Django nested if else in templatesDjango 在模板中嵌套 if else
【发布时间】:2016-04-10 11:10:21
【问题描述】:

我正在使用 django 嵌入视频,所以当用户放置 youtube 链接视频时,我可以使用视频的缩略图

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

但是当用户插入不是 youtube 视频的链接时,我想插入默认图片。

目前这就是我所拥有的

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

我得到上面的代码

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

在 {% if my_video%}

谁能帮帮我

这是一个嵌入视频应用的链接-> http://django-embed-video.readthedocs.org/en/v1.1.0/examples.html#template-examples

【问题讨论】:

    标签: python django


    【解决方案1】:

    {% if my video...%} 中的{% elif %} 没有任何条件。

    我认为你应该改用{% 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 %}
    

    基于 cmets 中的 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>
    

    【讨论】:

    • 我错了,我放了 elif,因为我上面有一些东西......我会发布完整的代码
    • 等待上面的代码和我没有的一样吗?我会检查
    • 我有一个 {% else %} 而不是 {% elif %}
    • 啊,谢谢,但我记得尝试过这个,但我遇到了一个错误......这是我的代码dpaste.com/2R00BPT,我得到 Invalid block tag: 'endvideo', expected 'elif', ' else' 或 'endif'....如果没有 {% endvideo %},我会得到预期的 endvideo,但我不明白
    • 您没有关闭您在 dpaste 上发布的代码中的视频标签
    猜你喜欢
    • 2018-12-07
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多