2012-02-10 32 views
20

Tôi có một trường chọn trong biểu mẫu và bây giờ tôi cần phải lặp qua các tùy chọn trong trường này.Làm cách nào để lặp qua các tùy chọn của một SelectField trong một mẫu?

{{ form.myselect }} mang lại cho tôi điều này:

<select name="myselect" id="id_myselect"> 
    <option value="" selected="selected">---------</option> 
    <option value="2">Item 1</option> 
    <option value="3">Item 2</option> 
    ... 
</select> 

Bây giờ tôi cần thêm một số thuộc tính cho các tùy chọn và vì lý do đó những gì tôi cần là:

<select name="myselect" id="id_myselect"> 
{% for x in form.myselect %} 
    <option value="{{ x.id }}">{{ x.name }}</option> 
{% endfor %} 
</select> 

nhưng có một lỗi:

Caught TypeError while rendering: 'BoundField' object is not iterable 

Tôi đã thử form.myselect.all, form.myselect.option_set nhưng nó không có gì

+0

Vì vậy, những gì bạn muốn là tất cả mà không có sự ' chứ không phải mỗi tùy chọn cánh đồng. Có thể lặp qua các lựa chọn với plugin này không? – pasevin

0

Với nút radio trong mẫu của bạn sử dụng.

<table> 
     {% for x,y in form.fields.Customer.choices %} 
     <tr> 
      <td><input id="id_Customer_{{x}}" name="Customer" type="radio" value="{{x}}" /></td> 
      <td>{{ y }}</td> 
     </tr> 
     {% endfor %} 
    </table> 
6

tôi làm theo cách này:

<select id="id_construction_type" name="construction_type" class="form-control input-md"> 
{% for value, key in form_urban.fields.construction_type.choices %} 
    <option value="{{ value }}"{% if form_urban.initial.construction_type == value %} selected {% endif %}> 
     {{ key }} 
    </option> 
{% endfor %} 
</select> 
Các vấn đề liên quan