2013-05-22 85 views
0

template.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 with display box will come display.It is a small logical错误,但我没有得到this.Help非常感谢。

+0

目前发生了什么?你已经告诉我们会发生什么,但目前没有发生什么?我认为即使'place'中没有值,也会显示'Display All'? –

回答

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> 

希望这有助于!

0
<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循环是在它的第一次迭代,如果所以它会仅每整个迭代一次显示输入。

+0

我尝试了您的代码,但它仍显示相同的内容,如果没有显示{{values}}中的值,则显示“显示全部”复选框正在显示。我想要的是,如果{{value}}中的值在显示器上显示,显示器将显示。 – user2086641

+0

'place'是怎么样的? {%if place%}检查地点是否有任何元素。 –

+0

列表中的内容。这就是我想知道的。 –