Вы можете расширить RadioInput
и RadioFieldRenderer
классы для проблемы и использовать MyCustomRenderer
в качестве средства визуализации
class MyRadioInput(RadioInput):
def __unicode__(self):
if 'id' in self.attrs:
label_for = ' for="%s_%s"' % (self.attrs['id'], self.index)
else:
label_for = ''
choice_label = conditional_escape(force_unicode(self.choice_label))
return mark_safe(u'<label%s>%s</label>%s' % (label_for, choice_label, self.tag()))
class MyCustomRenderer( RadioFieldRenderer ):
def __iter__(self):
for i, choice in enumerate(self.choices):
yield MyRadioInput(self.name, self.value, self.attrs.copy(), choice, i)
def __getitem__(self, idx):
choice = self.choices[idx] # Let the IndexError propogate
return MyRadioInput(self.name, self.value, self.attrs.copy(), choice, idx)
Если вы хотите вместо этого клиентскую часть решения:
<script>
$("label>input").each(function(index, item){
$(item).insertBefore($(item).parent())
})
</script>
Решение можно изменить, изменив django.forms.widgets.py
:
, прокрутите до class RadioInput
, мы изменим последнюю строку
def __unicode__(self):
return mark_safe(u'<label%s>%s %s</label>' % (label_for, self.tag(), choice_label)) #OLD
переходит к
return mark_safe(u'<label%s>%s</label>%s' % (label_for, choice_label, self.tag())) #NEW