Django form.cleaned_data是None

时间:2022-02-18 16:23:46

This is my form:

这是我的表格:

class EventForm(forms.Form):
    start_date       = forms.CharField(required=True, widget=forms.TextInput(attrs={'class': 'span6 as-date'}))
    end_date         = forms.CharField(required=True, widget=forms.TimeInput(attrs={'class': 'span6 as-date'}))
    title            = forms.CharField(required=True, widget=forms.TextInput(attrs={'class': 'span8'}))
    description      = forms.CharField(required=True, widget=forms.Textarea(attrs={'rows': 4, 'class': 'span8'}))
    total_seats      = forms.IntegerField(required=True, widget=forms.TextInput(attrs={'class': 'span6'}))
    buffer_seats     = forms.IntegerField(required=True, widget=forms.TextInput(attrs={'class': 'span6'}))
    seat_cost        = forms.IntegerField(required=True, widget=forms.TextInput(attrs={'class': 'span8'}))
    published        = forms.BooleanField(required=False, widget=forms.CheckboxInput(attrs={'class': 'span6'}))
    verified_booking = forms.BooleanField(required=False, widget=forms.CheckboxInput(attrs={'class': 'span6'}))

def clean(self):
    try:
        self.cleaned_data['start_date'] = datetime.strptime(self.cleaned_data['start_date'], '%Y/%m/%d %H:%M')
    except ValueError:
        raise forms.ValidationError('Improper start date')
    try:
        self.cleaned_data['end_date'] = datetime.strptime(self.cleaned_data['end_date'], '%Y/%m/%d %H:%M')
    except ValueError:
        raise forms.ValidationError('Improper end date')
    if self.cleaned_data['buffer_seats'] >= self.cleaned_data['total_seats']:
        raise forms.ValidationError('Seats buffered can not be same or greater than total seats')
    if self.cleaned_data['start_date'] >= self.cleaned_data['end_date']:
        raise forms.ValidationError('Event start date can not be greater than end date')
    if self.cleaned_data['published'] == 'on':
        self.cleaned_data['published'] = True
    else:
        self.cleaned_data['published'] = False
    if self.cleaned_data['verified_booking'] == 'on':
        self.cleaned_data['verified_booking'] = True
    else:
        self.cleaned_data['verified_booking'] = False

Here's the view:

这是观点:

event_form = EventForm(request.POST)
if event_form.is_valid():
   event = events_api.save_event_from_form(request.user, address, event_form.cleaned_data)

But I'm getting event_form.cleaned_data as None. What am I missing?

但我得到event_form.cleaned_data为None。我错过了什么?

1 个解决方案

#1


16  

form.clean() must return cleaned_data. The canonical implementation is:

form.clean()必须返回cleaning_data。规范的实现是:

class MyForm(forms.Form):
    # ...
    def clean(self):
        cleaned_data = super(MyForm, self).clean()
        # do your custom validations / transformations here
        # and some more
        return cleaned_data

#1


16  

form.clean() must return cleaned_data. The canonical implementation is:

form.clean()必须返回cleaning_data。规范的实现是:

class MyForm(forms.Form):
    # ...
    def clean(self):
        cleaned_data = super(MyForm, self).clean()
        # do your custom validations / transformations here
        # and some more
        return cleaned_data