Reputation: 3722
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
Upvotes: 0
Views: 230
Reputation: 6913
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)
Upvotes: 0
Reputation: 6620
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()
Upvotes: 4