Alex Rivera | Logout

How to use email instead of username for user authentication?

Asked 2009-04-01T05:26:28.923
15

Possible Duplicate:
Accepting email address as username in Django

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?

Edit
Report

1 Answer

6

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.

answered 2009-04-22T19:45:14.267

Your Answer