2011-01-31 7 views
1

안녕하세요 저는 항목 목록을 표시하는 템플릿 양식이 있습니다. 이 템플릿을 edit_order.html이라고합니다. 다른 항목 목록에서 새 항목을 추가 할 수 있기를 원합니다. 다른 항목 목록에서 items.html이라는 템플리트가 항목 목록을 표시합니다. items.html에서 각 항목에는 항목 외에도 확인란이 있습니다. 이제는 항목이 edit_order 템플릿에 이미 나열되어있는 경우에만 체크 박스를 표시하도록하고 싶습니다. 현재 모든 항목이 표시됩니다. 하지만 나는 이것을 원하지 않는다.Django checkbox 질문

{% extends "base_menu.html" %} 
    {%block script%} 
    <script type="text/javascript"> 
      $(function(){ 
        $("#check_all").click(function(){ 
          if(this.checked ==true) 
              $("tbody :checkbox").each(function(){ 
                this.checked=true; 
              }); 
            else 
              $("tbody :checkbox").each(function(){ 
                this.checked=false; 
              }); 
        }); 
      }); 
    </script> 
    {%endblock%} 


    <td><a href="{% url tiptop.views.edit_item item.client.pk item.pk %}" onclick="return showAddAnotherPopup(this);">Edit</a></td> 
     </tr> 
{% endfor %} 
</tbody> 
</table></fieldset> 
</div> 
<div id="form_footer"> 
       <input type="submit" value="Request Delivery" onclick="change_action('{% url tiptop.views.service_order client.pk 1 %}')"> 
       <input type="submit" value="Request Pick Up" onclick="change_action('{% url tiptop.views.service_order client.pk 2 %}');validate_status(this.form)"> 
     </div> 


</form> 
{% endblock %} 

     {% block right_content %} 
     <div id="location_header">{{client}}: Search results</div> 
     <form action="{% url tiptop.views.service_order client.pk 1 %}" method="post" onsubmit="return validate_selection(this)"> 
     <div class="form_container"> 
    <fieldset class="model"> 
    <table id="items_table"> 
      <thead> 
      <tr> 
        <th><input type="checkbox" id="check_all" checked="checked"></th> 
        <th scope="col">Tiptop no.</th><th scope="col">Client no.</th><th scope="col">Title</th><th scope="col">Type</th> 
        <th scope="col">Format</th><th scope="col">Status</th><th scope="col">Date</th> 
      </tr> 
      </thead> 
      <tbody> 
    {% for item in items %} 
      <tr class="items_table_row"> 
        <td><input type="checkbox" name="{{item.pk}}" value="{{item.pk}}" checked="checked"></td> 
        <td>{{item.tiptop_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td><td>{{item.type}}</td><td>{{item.format}}</td> 
        <td><span id="{{item.pk}}" name="type">{{item.itemstatushistory_set.latest}}</span></td><td>{{item.itemstatushistory_set.latest.date.date|date:"d M Y"}}</td> 

답변

3

item.html edit_order.html는

{% for item in items %} 
       <tr> 
       <td><input type="checkbox" name="item" value="{{item.pk}}" checked="checked"></td> 
       <td>{{item.tiptop_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td> 
       <td>{{item.type}}</td><td>{{item.format}}</td> 

       </tr> 
      {% endfor %} 

나는 당신이하려고하는 것과 같은 조금 혼란 스러워요. 그러나 가능한 경우 장부에 여러 폼 요소를 수동으로 렌더링하는 대신 Django에 포함 된 양식 라이브러리를 사용하는 것이 좋습니다. 다음은 사용자 정의/동적 선택이 확인란으로 렌더링 된 간단한 양식의 예입니다. '최초'가 양식 생성자에 주어진 kwarg 것을

>>> class CheckboxForm(forms.Form): 
...  items = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple) 
... 
>>> choices = (('item-1', 'This is item 1'), ('item-2', 'This is item 2'), ('item-3', 'This is item 3')) 
>>> form = CheckboxForm(initial={'items':('item-2',)}) 
>>> form.fields['items'].choices = choices 

>>> print form['items'] 
<ul> 
<li><label for="id_items_0"><input type="checkbox" name="items" value="item-1" id="id_items_0" /> This is item 1</label></li> 
<li><label for="id_items_1"><input checked="checked" type="checkbox" name="items" value="item-2" id="id_items_1" /> This is item 2</label></li> 
<li><label for="id_items_2"><input type="checkbox" name="items" value="item-3" id="id_items_2" /> This is item 3</label></li> 
</ul> 
>>> 

사항은 기본적으로 선택되는 요소의 ID의 반복자이어야한다 '항목'필드의 키를 가지고있다. 'items-2'가 'items'필드의 '초기'값으로 주어지고 결과 HTML 디스플레이에서 'item-2'가 선택되었는지 확인할 수 있습니다. 따라서이 'initial'인수를 사용자 정의하여 페이지에서 처음 확인할 항목을 지정할 수 있습니다.

장고 양식을 사용하는 경우 제출 된 양식 데이터의 유효성을 쉽게 확인할 수도 있습니다. 처음에 어떤 아이템이 선택되었는지는 중요하지 않으므로 입력 데이터에 바인딩 할 때 '초기'양식을 지정할 필요가 없습니다.

# valid submission 
>>> form = CheckboxForm({'items':('item-2',)}) 
>>> form.fields['items'].choices = choices 
>>> print form.is_valid() 
True 
>>> print form.cleaned_data 
{'items': [u'item-2']} 

# invalid submission, 'item-4' does not exist in the field choices 
>>> form = CheckboxForm({'items':('item-4',)}) 
>>> print form.is_valid() 
False 

참고 : 당신은 또한 설정 사용자 지정 폼의 생성자와는 대신 폼이 생성 된 후 field.choices을 설정으로 선택을 전달할 수 있습니다.

+0

저는 장고 양식을 사용하고 있습니다. 나는 내 견해를 올리지 않았다. 이미 체크리스트가 있습니다. 항목을 나열하는 템플릿이 있습니다. 이 템플릿에서 항목을 추가하고 싶습니다. 이제는 항목을 추가하여 양식을 만드는 것이 아닙니다. 내 말은, 기존의 모든 클라이언트 항목을 나열하는 페이지 (이 템플릿이 있음)로 이동한다는 것입니다. 이 템플릿에는 추가 할 기존 항목을 선택할 수있는 목록이 있습니다. 문제가있는 것은 체크 박스입니다. 그들은 어떤 이유로 든 내 코드에서 모두 '틱 (ticked)'합니다. 템플릿 항목 목록의 항목에 체크 표시를하고 빈 칸으로 표시합니다. – Shehzad009