tags:

views:

618

answers:

5

The authentication model provided along with Django is based on username.

What to do to change the authentication based on email instead of username?

To be more specific:

With username authentication, to login user we do the following:

 user = authenticate(name,password)
 .......
 login(request,user)

What to write for the above statements if we are authenticating using email?

For form:

I am planning to write my own form which shows the fields email, password and the validation. Is this the correct approach?

A: 

Sounds like you can just mask the username with the word "email" and all the usernames will just have the email show up instead.

jerebear
I think we can't use username field for email as we could not able to store @ symbol in the username.
Rama Vadakattu
Hmm....well, I don't know why not but if that's the case then my suggestion isn't very helpful.
jerebear
In Django 1.2, you @ is allowed on usernames, so this solution could work (and its pretty simple).
juanjux
+5  A: 

Check out this snippet, and read the comments for updates.

For the form, why not just inherit from (or directly use) the auth login form. See django/contrib/auth/forms.py

Van Gale
Also see the django-emailauth project: http://github.com/redvasily/django-emailauth/tree/master/ which hopefully will someday support EAUT style login as well.
Van Gale
+3  A: 

Please see the below link which illustrates the way in which we should solve the problem.

http://groups.google.com/group/django-users/browse_thread/thread/c943ede66e6807c

Rama Vadakattu
A: 

Unless I missed something, the solution should be really simple; just make a normal form with a text field and a password field. When the HTTP request method is POST, try to fetch the user with the given e-mail address. If such a user doesn't exist, you have an error. If the user does exist, try to authenticate the user and log her in.

Deniz Dogan
+3  A: 

I found this snippet when reading a duplicate question to this one. Also check this code:

class UserForm( forms.ModelForm ):
    class Meta:
        model= User
        exclude= ('email',)
    username = forms.EmailField(max_length=64,
        help_text = "The person's email address.")
    def clean_email( self ):
        email= self.cleaned_data['username']
        return email

class UserAdmin( admin.ModelAdmin ):
    form= UserForm
    list_display = ( 'email', 'first_name', 'last_name', 'is_staff' )
    list_filter = ( 'is_staff', )
    search_fields = ( 'email', )

admin.site.unregister( User )
admin.site.register( User, UserAdmin )


Neither answer is originally mine. Up vote on the other thread to the owners for the karma boost. I just copied them here to make this thread as complete as possible.

voyager