0

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

2 Answers 2

4

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()

Sign up to request clarification or add additional context in comments.

1 Comment

+1 you can only access the cleaned_data after the form has been validated
0

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)

1 Comment

Form.clean() is there so you can augment the normal clean functionality, which is used internally when you call .valid(). The question doesn't seem to be about form validation but rather about just accessing the cleaned_data. calling clean() is not the way to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.