I dont want my users to have to bother with a username. I want them to
authenticate with their email as their username. But django wont allow
valid @ signs (and other stuff) in a username. I could patch that but
that could bite me badly. So I found a better way.

First I create a manipulator:

from django.contrib.auth.forms import AuthenticationForm
class LoginManipulator(AuthenticationForm):

 and override the  __init__ and isValidUser methods. The __init__
method replaces the username field with an EmailField with name
'email'. Its just a cut n paste and replace job from django's code.
Then the 'isValidUser' validator looks like this:

    def isValidUser(self, field_data, all_data):
        """
        This is a modification of the AuthenticationForm validator that
uses
        the email rather than the username
        """
        try:
            self.user_cache = User.objects.get(email=field_data)
        except User.DoesNotExist:
            raise validators.ValidationError, _("Please enter a correct
email and password. Note that both fields are case-sensitive.")

 The only difference being the User.objects.get() line which gets the
User record by email rather than username.

 Then your login template form has this in it:

<tr><td><label for="id_email">Email:</label></td><td>{{
form.email}}</td></tr>

along with the password field. All seems to work nicely.

Possible problems occur if email isnt unique in the User table. We'll
be enforcing that at registration time, and I guess we can put that in
the database conditions too.

Anyone think of any other things that could go horribly wrong?

Barry


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to