tags:

views:

60

answers:

1

I'm trying to have a select form with a list of months but I can't seem to get it to post correctly.

form:

   class MonthForm(forms.Form):
        months = [('January','January'),
                  ('February','February'),
                  ('March','March'),
                  ('April','April'),
                  ('May','May'),
                  ('June','June'),
                  ('July','July'),
                  ('August','August'),
                  ('September','September'),
                  ('October','October'),
                  ('November','November'),
                  ('December','December'),]
        month = forms.ChoiceField(months)

view:

def table_view(request):
    if request.method == 'POST':
        month = MonthForm(request.POST).cleaned_data['month']
        print month

I keep getting this error:

'MonthForm' object has no attribute 'cleaned_data'
+5  A: 

The cleaned_data attribute is present only after the form has been validated with is_valid().

Just change your code to

def table_view(request):
    if request.method == 'POST':
        form = MonthForm(request.POST)
        if form.is_valid():
            print form.cleaned_data['month']

Internally, if any errors are detected, the cleaned_data attribute will be deleted (see forms/forms.py around line 270) and hence not accessible:

if self._errors:
    delattr(self, 'cleaned_data')

See also: http://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#processing-the-data-from-a-form

The MYYN
Interesting. I guess I didn't really think about validation for a select box Thanks
JPC