繁体   English   中英

Django:如果在列表模板标签中

[英]Django: if in list template tag

是否可以编写一个模板标签来检查列表的内容?

目前我从5到13进行了以下检查,但这非常详细,我需要做9次。

{% if wizard.steps.current == '5' %}                    
    <img src="{% static "survey/images/pathtwo/" %}{{display_image}}"/>                                                                     
    <section>   
    <span class="tooltip"></span>   
    <div id="slider"></div>  
    <span class="volume"></span>  
    </section>      
{% endif %}
{% if wizard.steps.current == '6' %}            
    <img src="{% static "survey/images/pathtwo/" %}{{display_image}}"/>                                                                     
    <section>   
    <span class="tooltip"></span>   
    <div id="slider"></div>  
    <span class="volume"></span>  
    </section>      
{% endif %}
    ...
    ...

我试过了

{% if wizard.steps.current in ['5','6','7','8','9','10','11','12','13'] %}              
    <img src="{% static "survey/images/paththree/" %}{{display_image}}" />                                                                      
    <section>   
    <span class="tooltip"></span>   
    <div id="slider"></div>  
    <span class="volume"></span>  
    </section>      
{% endif %} 

但是得到一个错误

例外值:无法解析余数:'['5','6','7','8','9','10','11','12','13']'来自' [ '5', '6', '7', '8', '9', '10', '11', '12', '13']”

有任何想法吗?

您可以尝试自己创建一个“In”过滤器。

# Somewhere in your template filters and tags

@register.filter
def InList(value, list_):
  return value in list_.split(',)

并在您的模板中:

{% load inlist %}

{% if not '1'|InList:'5,6,7,8,9,10,11,12,13' %}
<div>1 is not inside</div>
{% endif %}

{% if '5'|InList:'5,6,7,8,9,10,11,12,13' %}
<div>5 is inside</div>
{% endif %}

我刚试过它。 有用。

BR

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM