【发布时间】:2011-11-12 02:06:10
【问题描述】:
我想将 jquery ui 按钮集用于 django 表单中的无线电选择。我正在为 radioselect 小部件使用下面的自定义渲染器,但输入位于标签内。 jquery ui 不适用于该结构。我需要在渲染器中进行哪些更改才能将输入标签放在标签之前?
forms.py:
class SimpleRadioFieldRenderer(forms.widgets.RadioFieldRenderer):
def render(self):
"""Outputs widget without <ul> or <li> tags."""
return mark_safe(u'\n'.join([u'%s'
% force_unicode(w) for w in self]))
class MyForm(Form):
myradiofield = ChoiceField(
widget=RadioSelect(renderer=SimpleRadioFieldRenderer),
required=True,
choices=(('a','one choice'), ('b','another choice')))
以下是 django 在模板中的呈现方式:
<label for="id_myradiofield_0"><input type="radio" name="myradiofield" value="a" id="id_myradiofield_0"> one choice</label>
<label for="id_myradiofield_1"><input type="radio" name="myradiofield" value="b" id="id_myradiofield_0"> another choice</label>
这是 jquery ui 期望的样子:
<input type="radio" name="myradiofield" value="a" id="id_myradiofield_0"><label for="id_myradiofield_0"> one choice</label>
<input type="radio" name="myradiofield" value="b" id="id_myradiofield_0"> <label for="id_myradiofield_1">another choice</label>
提前感谢您的帮助。
【问题讨论】: