Vì vậy, tôi đang sử dụng giao diện người dùng jQuery đến skin the radio buttons nhưng tôi không thể tải Django để biểu mẫu của tôi theo cách mà nó phải được thực hiện.Tùy chỉnh biểu mẫu Django với phụ kiện RadioSelect
tôi cần phải có cấu trúc này:
<table>
<tr>
<td><label for="notify_new_friends">Notify when new friends join</label></td>
<td class="radio">
<input type="radio" name="notify_new_friends" id="notify_new_friends_immediately" value="1" checked="checked"/><label for="notify_new_friends_immediately">Immediately</label>
<input type="radio" name="notify_new_friends" id="notify_new_friends_never" value="0"/><label for="notify_new_friends_never">Never</label>
</td>
</tr>
</table>
Vì vậy, để tóm tắt rằng tôi cần các nút radio trong một lớp học (radio), nơi họ có một input
và label for
.
Khi tôi làm cho hình thức trong mẫu của tôi với {{ profile_form.notify_new_friends }}
tôi nhận được như sau:
<ul>
<li><label for="id_notify_new_friends_0"><input type="radio" id="id_notify_new_friends_0" value="0" name="notify_new_friends" /> Immediately</label></li>
<li><label for="id_notify_new_friends_1"><input type="radio" id="id_notify_new_friends_1" value="1" name="notify_new_friends" /> Never</label></li>
</ul>
Đó là chính xác những gì tôi muốn trừ danh sách phần. Vì vậy, tôi đã cố gắng lặp trên nó mang đến cho tôi những nhãn định dạng khác nhau:
{% for item in profile_form.notify_new_friends %}
{{ item }}
{% endfor %}
mà mang lại cho tôi:
<label><input type="radio" name="notify_new_friends" value="0" /> Immediately</label>
<label><input type="radio" name="notify_new_friends" value="1" /> Never</label>
Vì vậy, vấn đề ở đây là nó dừng sử dụng label for
và bắt đầu sử dụng chỉ label
để wrapp nó tất cả với.
Tôi cũng đã thử làm một cái gì đó như thế này, nhưng sau đó label
và label_tag
không hiển thị bất kỳ thứ gì.
{{ profile_form.notify_new_friends.0 }}
{{ profile_form.notify_new_friends.0.label_tag }}
{{ profile_form.notify_new_friends.0.label }}
Vì vậy, có ai biết cách tôi có thể hiển thị đúng cách này không !?
FYI, đây là forms.py tôi:
self.fields['notify_new_friends'] = forms.ChoiceField(label='Notify when new friends join', widget=forms.RadioSelect, choices=NOTIFICATION_CHOICES)
https://docs.djangoproject.com/en/dev/ref/forms/widgets/#radioselect - bạn đã đọc sách này chưa? –
@ AlexanderA.Sosnovskiy có, nhưng tôi đã bỏ lỡ 'choice_label' để lấy tiêu đề nhãn, nhưng tiếc là làm' {{radio.tag}} 'không cung cấp cho tôi ID và do đó tôi không thể thực hiện' label for' mà tôi cần phải làm. – olofom
id trường được tự động tạo thử, {{field.auto_id}} –