Tuesday, January 29, 2013

Help, getting keyerrors

Hello,
On my registration form, if I enter no data and just press enter, i get a key error.
Can someone tell me what I am doing wrong?
Thanks

forms.py
from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from customer.models import Customer

class RegistrationForm(forms.Form):
    username = forms.CharField(label=u'User Name')
    firstname = forms.CharField(label=u'First Name')
    lastname = forms.CharField(label=u'Last Name')
    email = forms.EmailField(label=u'Email Address')
    company = forms.CharField(label=u'Company')
    phone = forms.CharField(label=u'Phone')
    password = forms.CharField(label=u'Password',widget=forms.PasswordInput(render_value=False),required=True)
    password1 = forms.CharField(label=u'Verify Password',widget=forms.PasswordInput(render_value=False),required=True)

    def clean_username(self):
        username = self.cleaned_data['username']
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return self.cleaned_data['username']
        raise forms.ValidationError('That username is already taken, please select another.')

    def clean(self):
        password = self.clean_password()
        password1 = self.cleaned_data['password1']
        if password and password1 and password == password1:
            return self.cleaned_data
        else:
            raise forms.ValidationError('The passwords did not match, Please try again.')

    def clean_company(self):
        return self.cleaned_data['company']

    def clean_phone(self):
        return self.cleaned_data['phone']

    def clean_email(self):
        return self.cleaned_data['email']

    def clean_firstname(self):
        return self.cleaned_data['firstname']

    def clean_lastname(self):
        return self.cleaned_data['lastname']

    def clean_password(self):
        if self.password and self.cleaned_data['password']:
            return self.cleaned_data['password']
        else:
            raise forms.ValidationError("Enter a password.")


    def clean_password1(self):
        if not self.cleaned_data['password1']:
            raise forms.ValidationError("Enter your password (again)")


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment