如何为模板中的变量赋值?

时间:2014-04-11 13:34:22

标签: python django

我有以下代码:

<img src = "{{MEDIA_URL}} {{get_item_news.image}}" alt = "" style = "float: 
{% If get_item_news.inner_image_position == 'left'%} 
left 
{% Elif get_item_news.inner_image_position == 'right'%} 
right 
{% Endif%} 
"/> 

求助请移动if-endif属性块并将它们放在img

之前

2 个答案:

答案 0 :(得分:1)

这应该有效:

{% with get_item_news.inner_image_position as alignment %}
  <img src = "{{MEDIA_URL}} {{get_item_news.image}}"
       alt = "" style = "float: {{ alignment }};"/>
{% endwith %}

答案 1 :(得分:0)

试试这个,

{% if get_item_news.inner_image_position == 'left'%}
       <img src = "{{MEDIA_URL}} {{get_item_news.image}}" alt = "" style = "float:left"/> 
{%else%}
       <img src = "{{MEDIA_URL}} {{get_item_news.image}}" alt = "" style = "float:right"/> 
{%endif%}