【问题标题】:Amend django-template if statement to ignore duplicates修改 django-template if 语句以忽略重复项
【发布时间】:2015-08-04 09:01:59
【问题描述】:

如何更改 for 循环以忽略重复项 mealtype == 'Entrees'

我只需要创建1个<a href>

{% for menu in menus %}
            {% if menu.mealtype == 'Entrees' %}
            <li role="presentation">
                <a href="#tab1" aria-controls="tab1" role="tab" data-toggle="tab">Entrees</a>
            </li>
            {% endif %}
{% endfor %}

满满的

{% for menu in menus %}
    {% if menu.show_presentation %}
    <li role="presentation">
        <a href="#tab1" aria-controls="tab1" role="tab" data-toggle="tab">Entrees</a>
    </li>
    {% endif %}

    {% if menu.show_presentation %}
    <li role="presentation">
        <a href="#tab2" aria-controls="tab2" role="tab" data-toggle="tab">Sides</a>
    </li>
    {% endif %}

    {% if menu.show_presentation %}
    <li role="presentation">
        <a href="#tab3" aria-controls="tab3" role="tab" data-toggle="tab">Mains</a>
    </li>
    {% endif %}

    {% if menu.show_presentation %}
    <li role="presentation">
        <a href="#tab4" aria-controls="tab4" role="tab" data-toggle="tab">Drinks</a>
    </li>
    {% endif %}

    {% if menu.show_presentation %}
    <li role="presentation">
        <a href="#tab5" aria-controls="tab2" role="tab" data-toggle="tab">Desserts</a>
    </li>
    {% endif %}

    {% if menu.show_presentation %}
    <li role="presentation">
        <a href="#tab6" aria-controls="tab3" role="tab" data-toggle="tab">Specials</a>
    </li>
    {% endif %}

    {% if menu.show_presentation %}
    <li role="presentation">
        <a href="#tab7" aria-controls="tab4" role="tab" data-toggle="tab">Others</a>
    </li>
    {% endif %}
{% endfor %}

【问题讨论】:

    标签: html django django-templates django-views


    【解决方案1】:

    更新基于 cmets 的视图。试试这个:

    show_presentation_list = []
    menus_presentation = []
    
    for menu in menus:
        if menu.mealtype and menu.mealtype not in show_presentation_list:
            show_presentation_list.append(menu.mealtype)
            menus_presentation.append(menu)
    

    还有你的新模板,试试这个:

    {% for menu in menus_presentation %}
        <li role="presentation">
                <a href="#tab{{forloop.counter}}" aria-controls="tab{{forloop.counter}}" role="tab" data-toggle="tab">{{menu.mealtype}}</a> 
        </li>
    {% endfor %}
    

    【讨论】:

    • 谢谢。让它忽略所有重复项。我确实有其他餐食类型 - 主菜、配菜、主菜等。我现在遇到的问题是,即使没有该特定餐食类型的条目,它仍然显示 li。我尝试使用for menu in menus: if menu.mealtype is None: menu.show_presentation = False elif menu.mealtype == 'Entrees' or 'Sides' or 'Mains' or 'Drinks' or 'Desserts' or 'Specials' or 'Others': menu.show_presentation = True break,但似乎不起作用
    • 我不明白。您想为每种膳食类型显示一次(并删除重复项)还是仅对主菜显示 li?
    • 我刚刚添加了完整的 li。目前确实会显示每种膳食类型一次并删除重复项。但是,如果 li 为空,我希望 li 根本不显示
    • 用更可重用的方法更新了答案
    • 感谢伙伴,工作完美,比我拥有的更干净
    猜你喜欢
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2017-05-25
    • 2015-11-27
    相关资源
    最近更新 更多