【问题标题】:Template logic not functioning properly模板逻辑无法正常运行
【发布时间】:2013-05-22 10:38:48
【问题描述】:

模板.html

<tr><td>
    {% if place %}                                
      <input type="checkbox" id="select_all"/>Display all<br />
      <hr style="width: 150px;margin: 8px 0;">
      {% for value in place %}
      {{ value }}
      {% endfor %}
    {% endif %}</td></tr>
<tr><td>{% include "buttons/addalist.html" %} {% include "buttons/save.html" %}</td></tr>

views.py

def type_list(request,type_id):
    user = request.user
    try:
        type_list = Types.objects.get(user=user.id, id=type_id)
    except:
        return redirect('incident.views.incident_types')
    if request.method == 'POST':
        report_type = TypeSettingsForm(request.POST)

        if 'title' in request.POST and report_type.is_valid():
            if request.POST['title'].strip():
                result = report_type.save(commit=False)
                result.user = user
                result.is_active = True
                result.parent_type_id = type_id
                result.save()
        else:
            place = TypeSelectionForm(type_id, request.POST)
            if types.is_valid() and 'status' in request.POST:
                types.save(type_id, request.POST)
                type_list.is_active = eval(request.POST['status'])
                type_list.save()

                return redirect('incident.views.incident_types')
    place = TypeSelectionForm(type_id)

    return render(request, 'incident/type_list.html',
        {
            'about_menu': True,
            'type_list': type_list,
            'place':place
    })

点击addalist按钮,值被保存并显示在模板中。

但最初,如果它们没有从 for 循环显示的值,则不应显示带有复选框的 Display All。如果输入来自 for 循环的单个值,则将显示带有复选框的 Display All。这是一个小逻辑错误,但我没有得到这个。非常感谢帮助。

【问题讨论】:

  • 目前发生了什么?你告诉我们应该发生什么,但没有告诉我们目前正在发生什么?我认为即使place 中没有值也会显示Display All

标签: django django-models django-templates django-admin


【解决方案1】:

您可以在视图count_check_boxes 内创建一个变量,该变量计算表单具有的选项数量并在您的模板中对其进行测试。

def type_list(request,type_id):
    ...
    place = TypeSelectionForm(type_id)
    count_check_boxes = len(place.fields['checkbox_field'].choices)
    return render(request, 'incident/type_list.html',
        {
            'about_menu': True,
            'type_list': type_list,
            'place':place,
            'check_boxes_count':check_boxes_count
    })

在你的模板中:

<tr><td>
    {% if place %}                                
      {%if check_boxes_count > 0%}
      <input type="checkbox" id="select_all"/>Display all<br />
      <hr style="width: 150px;margin: 8px 0;">
      {%endif%}
      {% for value in place %}
      {{ value }}
      {% endfor %}
    {% endif %}</td></tr>
<tr><td>{% include "buttons/addalist.html" %} {% include "buttons/save.html" %}</td></tr>

希望这会有所帮助!

【讨论】:

    【解决方案2】:
    <tr><td>
        {% if place %}      
            {% for value in place %}
                {% if forloop.first %}
                <input type="checkbox" id="select_all"/>Display all<br />
                <hr style="width: 150px;margin: 8px 0;">
                {% endif %}
                {{ value }}
            {% endfor %}
        {% endif %}</td></tr>
    <tr><td>{% include "buttons/addalist.html" %} {% include "buttons/save.html" %}</td></tr>
    

    使用 forloop.first 检查 for 循环是否在其第一次迭代中,如果是,则在整个迭代中只显示一次输入。

    【讨论】:

    • 我尝试了你的代码,但它仍然显示相同,如果没有来自 {{values}} 的值显示“显示全部”复选框正在显示。我想要的是如果来自 {{ value }} 的值即将显示,Display All 应该显示。
    • place 长什么样子? {% if place %} 检查 place 中是否有任何元素。
    • list 的地方有什么。这就是我想知道的。
    猜你喜欢
    • 1970-01-01
    • 2020-08-15
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多