reg/forms.py
author nishanth
Fri, 09 Apr 2010 11:46:35 +0530
changeset 3 182f216da4a8
child 4 ededea9ad08b
permissions -rw-r--r--
made the login view. have to write templates and check it.

from django.contrib.auth.models import User
from django import forms

from django.contrib.auth import authenticate

class LoginForm(forms.ModelForm):
    """ a form to handle login.
    """

    class Meta:
        model = User
        fields = ['email', 'password']

    def clean_email(self):
        """ see if a user exists for this email.
        """

        email = self.cleaned_data['email']
        try:
            self.user = User.objects.get(email__iexact=email)
            return email
        except User.DoesNotExist:
            raise forms.ValidationError("Incorrect e-mail or password")

    def clean_password(self):
        """ now we know that the user exists.
        we see if he typed the password correctly.
        """

        password = self.cleaned_data['password']
        user = authenticate(self.user.username, password)
        if not user:
            raise forms.ValidationError("Incorrect e-mail or password")
        return password