开发者

problem with django forms

i have the following django form . which has a choice field. im trying to get the value selected in the radioSelect widget . im using (cleaned_data) method for this task. but it cant access 开发者_开发知识库the method cleaned_data for some reason.

Exception Value :'AnswersForm' object has no attribute 'cleaned_data'

forms.py:


class AnswersForm(forms.Form):

        CHOICES=[('sf','asdf')]
        answers = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect())


and in views.py:


    form = forms.AnswersForm()
    form.cleaned_data['answers']


does anyone know what is going on ? or is there another way to perform this ?

thanks in advance


You don't show any code where you pass data to the form or check if it is valid. Normal practice is something like:

if request.method == 'POST':
    form = Forms.AnswersForm(request.POST)
    if form.is_valid():
        do_something_with(form.cleaned_data['answers'])

You cannot access cleaned_data until you call is_valid()


Use Form.clean() It just returns self.cleaned_data

Note: Clean the data in the forms.py

def clean(self,):
  Here you can clean the form.

**For Reference ** Go Here (Django doc)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜