Template rendering issue with RadioSelect() widget in Django
I am trying to render a (default) radio button by using the RadioSelect() widget 开发者_运维问答provided in django but whenever I use it the output is blank. But if I let the django forms to load the default renderer than it prints out a dropdown box with the choices just fine. I am pasting here the summary of code that I have written for this.
# In models.py
AUDIO_SETTING_CHOICES = (
('RESTART', 'Play audio from the beginning.'),
('CONTINUE', 'Continue playing previous audio (if same).'),
)
audio_setting = models.CharField(max_length=20, choices=AUDIO_SETTING_CHOICES, default='RESTART')
# In forms.py
class ChapterItemForm(forms.ModelForm):
def __init__(self, user=None, *args, **kwargs):
self.user = user
super(ChapterItemForm, self).__init__(*args, **kwargs)
self.fields['audio_setting'] = forms.ChoiceField(label="How to play the audio?",
widget=forms.RadioSelect())
# In template
<Form>
...
audio setting: {{ form.audio_setting }} <br />
...
So any pointers on what I might be doing wrong?
http://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield
you aren't passing any choices to the ChoiceField
self.fields['audio_setting'] = forms.ChoiceField(
choices=AUDIO_SETTING_CHOICES, label="How to play the audio?",
widget=forms.RadioSelect())
精彩评论