简体   繁体   中英

Missing cleaned_data in forms (django)

I would like to create a form and the validation_forms that would check if some text apears in a box if another box has been checked correctly,

class Contact_form(forms.Form):

def __init__(self):

    TYPE_CHOICE = (
    ('C', ('Client')),
    ('F', ('Facture')),
    ('V', ('Visite'))
    )

    self.file_type = forms.ChoiceField(choices = TYPE_CHOICE, widget=forms.RadioSelect)
    self.file_name = forms.CharField(max_length=200)
    self.file_cols = forms.CharField(max_length=200, widget=forms.Textarea)
    self.file_date = forms.DateField()
    self.file_sep = forms.CharField(max_length=5, initial=';')
    self.file_header = forms.CharField(max_length=200, initial='0')

    def __unicode__(self):
    return self.name

    # Check if file_cols is correctly filled
    def clean_cols(self):
        #cleaned_data = super(Contact_form, self).clean() # Error apears here
    cleaned_file_type = self.cleaned_data.get(file_type)
    cleaned_file_cols = self.cleaned_data.get(file_cols)

    if cleaned_file_type == 'C':
        if 'client' not in cleaned_file_cols:
            raise forms.ValidationError("Mandatory fields aren't in collumn descriptor.")
    if cleaned_file_type == 'F':
        mandatory_field = ('fact', 'caht', 'fact_dat')
        for mf in mandatory_field:
            if mf not in cleaned_file_cols:
                raise forms.ValidationError("Mandatory fields aren't in collumn descriptor.")

def contact(request):

contact_form = Contact_form()
contact_form.clean_cols()
return render_to_response('contact.html', {'contact_form' : contact_form})

Infortunatly, django keeps saying me that he doesn't reconize cleaned_data. I know i've missed something about the doc or something but i cannot get the point on what. Please help !

When validating an individual field, your clean method should have a name of the form

clean_<name of field>

for example clean_file_col . Then it will be called automatically when you do form.is_valid() in your view.

Naming your method clean_cols suggests that you have a field named cols , which could cause confusion.

In this case, your validation relies on other fields , so you should rename your clean_col method to simply clean . That way it will be called automatically when you do form.is_valid() in your view.

def clean(self):
    cleaned_data = super(Contact_form, self).clean()
    cleaned_file_type = self.cleaned_data.get(file_type)
    # ...

Finally, in your view, you have not bound your form to any data,

contact_form = Contact_form()

so contact_form.is_valid() will always return False. You need to bind your form to the post data with form = ContactForm(request.POST) . See the Django docs for using a form in a view for a full example and explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM