Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django模板语言if和else语句,if工作,else不工作';我什么也没表现出来_Python_Django - Fatal编程技术网

Python django模板语言if和else语句,if工作,else不工作';我什么也没表现出来

Python django模板语言if和else语句,if工作,else不工作';我什么也没表现出来,python,django,Python,Django,这就是我要做的。用户可以插入url,如果该url是youtube链接,我希望显示视频缩略图。(我使用的是django嵌入视频)如果该url不是youtube链接,我希望显示其他图像(post.image)。所以我用了else,if语句 {% if post.url %} {% video post.url as my_video %} {% if my_video %} <img src="{{ my_video.thumbnail }}" class="img-

这就是我要做的。用户可以插入url,如果该url是youtube链接,我希望显示视频缩略图。(我使用的是django嵌入视频)如果该url不是youtube链接,我希望显示其他图像(post.image)。所以我用了else,if语句

 {% 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 %}
您可以尝试以下方法:

 {% 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 %}
{%if post.url%}
{%video post.url作为我的视频%}
{%if my_video%}
{%else%}
{%endif%}
{%endif%}

首先,您需要上传post.image

能否显示
视频
模板标签定义?谢谢。@alecxe谢谢您的回复,您是否也可以在这两种情况下显示
my\u video
的值,因为您正在检查
{%if my\u video%}
。您确定要通过检查此条件或作为
post.url
check的else块来显示图像。@AKS很抱歉,我对您的问题有点困惑,两者都是什么意思?我正在检查我的视频是否在那里,如果在那里我想显示x,否则显示y。我该怎么检查?@AKS现在我明白你这两种情况的意思了,我试过{%else my_video%}我没有收到异常消息Supplied这不起作用…我认为上面的代码不起作用,因为图像不是video/mp4,我不认为缩略图像之间需要标记。是否要运行视频或图像?这正是我所做的,只是我不需要{{media\u url},如上所述,显示良好
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
 {% 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 %}