reg/forms.py
author nishanth
Fri, 09 Apr 2010 12:28:58 +0530
changeset 4 ededea9ad08b
parent 3 182f216da4a8
child 6 057498d12450
permissions -rw-r--r--
login and logout works .

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

from django.contrib.auth import authenticate

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

    email = forms.EmailField()
    password = forms.CharField(widget=forms.PasswordInput)

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

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

        user = authenticate(username=username, password=password)
        if not user:
            raise forms.ValidationError("Incorrect e-mail or password")
        return email