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

Dee*_*end 3 django

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

目前我从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 %}
    ...
    ...
Run Code Online (Sandbox Code Playgroud)

我试过了

{% 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 %} 
Run Code Online (Sandbox Code Playgroud)

但是得到一个错误

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

有任何想法吗?

小智 5

您可以尝试自己创建一个"In"过滤器.

# Somewhere in your template filters and tags

@register.filter
def InList(value, list_):
  return value in list_.split(',)
Run Code Online (Sandbox Code Playgroud)

并在您的模板中:

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

我刚试过它.有用.

BR