Using email instead of username for registration and login

706 views
Skip to first unread message

Bill Beal

unread,
Sep 24, 2012, 9:57:20 PM9/24/12
to django...@googlegroups.com
Hi all,

I want to use the email address as the username for registration and login.  I'm using django-registration for 2-stage registration.  I'm looking for an easier way than what I've come up with so far.  I can modify registration and activation, but then django.contrib.auth.views.login has a 30-character limit on the username.  I'm not looking forward to making username act like an email address.  Any quick fixes?

Kurtis Mullins

unread,
Sep 24, 2012, 10:13:43 PM9/24/12
to django...@googlegroups.com
You could check out django-userena

On Mon, Sep 24, 2012 at 9:57 PM, Bill Beal <b.b...@eximflow.com> wrote:
Hi all,

I want to use the email address as the username for registration and login.  I'm using django-registration for 2-stage registration.  I'm looking for an easier way than what I've come up with so far.  I can modify registration and activation, but then django.contrib.auth.views.login has a 30-character limit on the username.  I'm not looking forward to making username act like an email address.  Any quick fixes?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/E4GTF1wAPZ8J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Russell Keith-Magee

unread,
Sep 24, 2012, 11:02:27 PM9/24/12
to django...@googlegroups.com
The quick and nasty fix -- issue the ALTER statement on your database
to make the field longer, and define custom forms that enforce the new
field length. Provide those custom forms to the auth login views, etc.

The slightly better fix -- fork Django (or, at least,
django.contrib.auth) for the purposes of your local deployment, and
modify the 30 character constraint wherever it occurs.

The real fix: I'm about to land a new feature for Django 1.5 that will
allow you to install a custom your User model that has whatever
properties you want (e.g., a longer username field, only an email
field, twitter handle instead of username, etc). If you want to test
this branch, you can check out my Github repo [1], or wait a day or
two and try it out on the Django development branch (that will
eventually become 1.5).

[1] https://github.com/freakboy3742/django/tree/t3011

Yours,
Russ Magee %-)

Stephen Anto

unread,
Sep 25, 2012, 12:54:09 AM9/25/12
to django...@googlegroups.com
Hi,

For login with email or username in Django visit http://www.f2finterview.com/web/Django/18/ it will give you direction

On Tue, Sep 25, 2012 at 7:27 AM, Bill Beal <b.b...@eximflow.com> wrote:
Hi all,

I want to use the email address as the username for registration and login.  I'm using django-registration for 2-stage registration.  I'm looking for an easier way than what I've come up with so far.  I can modify registration and activation, but then django.contrib.auth.views.login has a 30-character limit on the username.  I'm not looking forward to making username act like an email address.  Any quick fixes?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/E4GTF1wAPZ8J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Thanks & Regards
Stephen S



Blog:      blog.f2finterview.com

Tom Christie

unread,
Sep 25, 2012, 11:01:02 AM9/25/12
to django...@googlegroups.com
This should cover it:

  https://github.com/dabapps/django-email-as-username

For compatibility with django-registration you'll also want to take a look at the thread on this ticket:


  Tom

Germán

unread,
Sep 26, 2012, 2:14:42 PM9/26/12
to django...@googlegroups.com
I opted to customize a little.

First the backend:

from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User


class EmailBackend(ModelBackend):
"""A django.contrib.auth backend that authenticates the user based on its
email address instead of the username.
"""

def authenticate(self, email=None, password=None):
"""Authenticate user using its email address instead of username."""
try:
user = User.objects.get(email=email)
if user.check_password(password):
return user
except User.DoesNotExist:
return None

Then setup settings accordingly (notice that I wrote the backend inside an app called 'accounts'):

AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # necessary for django.auth
'accounts.backends.EmailBackend' # custom backend to authenticate using the email field
)

And then modify your login view:

if request.method == 'POST' and username and password:
user = auth.authenticate(username=username, password=password)
if user is None:
user = auth.authenticate(email=email, password=password)

What do you think? I know it is not that simple but I couldn't figure out an easier/cleaner way to do it.
Reply all
Reply to author
Forward
0 new messages