Django中的RadioSelect()小部件的模板渲染问题

时间:2011-03-20 10:38:18

标签: django django-forms radio-button

我试图通过使用django中提供的RadioSelect()小部件来渲染(默认)单选按钮,但每当我使用它时,输出都是空白的。但是,如果我让django表单加载默认渲染器,而不是打印出一个带有选择的下拉框就好了。我在这里粘贴了我为此编写的代码摘要。

# 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 /> 
...

所以关于我可能做错的任何指示?

1 个答案:

答案 0 :(得分:2)

http://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield

您没有向ChoiceField传递任何选择

self.fields['audio_setting'] = forms.ChoiceField(choices=AUDIO_SETTING_CHOICES, label="How to play the audio?", widget=forms.RadioSelect())