<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>David Cramer's Blog - Latest Comments in Logging In With Email Addresses in Django</title><link>http://davidcramer.disqus.com/</link><description></description><atom:link href="https://davidcramer.disqus.com/logging_in_with_email_addresses_in_django/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Mon, 06 Feb 2012 01:22:14 -0000</lastBuildDate><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-430623479</link><description>&lt;p&gt;I found the following solution to the 30 character limitation on the username field, mentioned below.&lt;br&gt;&lt;a href="http://stackoverflow.com/questions/6780701/forcing-django-login-form-to-take-username-over-30-characters" rel="nofollow noopener" target="_blank" title="http://stackoverflow.com/questions/6780701/forcing-django-login-form-to-take-username-over-30-characters"&gt;http://stackoverflow.com/qu...&lt;/a&gt; &lt;br&gt;It seems to work for me.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Serge</dc:creator><pubDate>Mon, 06 Feb 2012 01:22:14 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-181191761</link><description>&lt;p&gt;This is silly. Are the Django maintainers just really lazy or do they need a new rcs to make applying submitted patches easier? At this point nearly every major new web application uses email as the primary means of authentication. Why can this not make it into django core and instead requires a custom backend?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">br</dc:creator><pubDate>Fri, 08 Apr 2011 18:52:47 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-85190571</link><description>&lt;p&gt;I'm new to Django and still learning, but what's wrong with just overriding django's AuthenticationForm and using that in your login view:&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;pre&gt;from django.contrib.auth.forms import AuthenticationForm&lt;br&gt;class MyAuthenticationForm(AuthenticationForm):&lt;br&gt;    username = forms.EmailField(help_text="User name is email address",&lt;br&gt;    label="User name:", max_length=30) &lt;br&gt;&lt;br&gt;def my_login(request):&lt;br&gt;    if request.method == 'POST':&lt;br&gt;        form = MyAuthenticationForm(data=request.POST)&lt;br&gt;        if form.is_valid():&lt;br&gt;            user = form.get_user() &lt;br&gt;... etc&lt;br&gt;&lt;br&gt;&lt;/pre&gt;&lt;p&gt;Seems like you could also do the same thing with django's UserCreation form to enforce valid email addresses as logins. Or am I missing something (certainly possible).&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">adj7388</dc:creator><pubDate>Fri, 08 Oct 2010 09:50:26 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-79025252</link><description>&lt;p&gt;Another approach to solve this problem: &lt;a href="http://gist.github.com/586056" rel="nofollow noopener" target="_blank" title="http://gist.github.com/586056"&gt;http://gist.github.com/586056&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Rainer Borene</dc:creator><pubDate>Sat, 18 Sep 2010 17:35:08 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-77836754</link><description>&lt;p&gt;Attempting a better format job of the code below&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;pre&gt;from django.contrib.auth.backends import ModelBackend # default backend&lt;br&gt;&lt;br&gt;&lt;br&gt;# Overwrite the default backend to check for e-mail address &lt;br&gt;class EmailBackend(ModelBackend):&lt;br&gt;&lt;br&gt;    def authenticate(self, username=None, password=None):&lt;br&gt;        #If username is an email address, then try to pull it up&lt;br&gt;        if email_re.search(username):&lt;br&gt;            try:&lt;br&gt;                user = User.objects.get(email=username)&lt;br&gt;            except User.DoesNotExist:&lt;br&gt;                return None&lt;br&gt;&lt;br&gt;        else:&lt;br&gt;            #We have a non-email address username we should try username&lt;br&gt;            try:&lt;br&gt;                user = User.objects.get(username=username)&lt;br&gt;            except User.DoesNotExist:&lt;br&gt;                return None&lt;br&gt;&lt;br&gt;        if user.check_password(password):&lt;br&gt;            return user&lt;br&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nick</dc:creator><pubDate>Wed, 15 Sep 2010 14:21:02 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-77836695</link><description>&lt;p&gt;I ran into a problem with the above code - it doesn't support permissions. Rather than creating an entirely new backend, I found the better solution to be to extend the default, and simply override the 'authenticate' method, so that all the built in functionality is preserved.&lt;/p&gt;&lt;p&gt;from django.contrib.auth.backends import ModelBackend # default backend&lt;/p&gt;&lt;p&gt;# Overwrite the default backend to check for e-mail address &lt;br&gt;class EmailBackend(ModelBackend):&lt;/p&gt;&lt;p&gt;    def authenticate(self, username=None, password=None):&lt;br&gt;        #If username is an email address, then try to pull it up&lt;br&gt;        if &lt;a href="http://email_re.search" rel="nofollow noopener" target="_blank" title="email_re.search"&gt;email_re.search&lt;/a&gt;(username):&lt;br&gt;            try:&lt;br&gt;                user = User.objects.get(email=username)&lt;br&gt;            except User.DoesNotExist:&lt;br&gt;                return None&lt;/p&gt;&lt;p&gt;        else:&lt;br&gt;            #We have a non-email address username we should try username&lt;br&gt;            try:&lt;br&gt;                user = User.objects.get(username=username)&lt;br&gt;            except User.DoesNotExist:&lt;br&gt;                return None&lt;/p&gt;&lt;p&gt;        if user.check_password(password):&lt;br&gt;            return user&lt;/p&gt;&lt;p&gt;This allows the built in decorators like @permission_required to work.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nick</dc:creator><pubDate>Wed, 15 Sep 2010 14:20:42 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-77508193</link><description>&lt;p&gt;Well you don't have to do it directly puting the email on username field. I used, on user registration, creating a username that's a 30 chars long hash of the email. So then you can simply log the user checking his email hash with the value on username field on database. You must overwrite the auth backend to do that.&lt;br&gt;In this way you can pass the username field limits.&lt;/p&gt;&lt;p&gt;Sorry for my english :(&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Stefan Manastirliu</dc:creator><pubDate>Tue, 14 Sep 2010 09:48:42 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-72684046</link><description>&lt;p&gt;There are still some issues with this approach:&lt;/p&gt;&lt;p&gt;1) Username field is restricted by username CharField's max_length property. Even if you override form validation, DB will have varchar(30) field unless you alter your table manually.&lt;br&gt;2) You need to store your email data both in username and email field to make User.email_user() working. I think there are some other places, when &lt;a href="http://User.email" rel="nofollow noopener" target="_blank" title="User.email"&gt;User.email&lt;/a&gt; is used.&lt;br&gt;3) Code readability fail. Sure, other djangonauts know about this pitfall, but treating field called 'username' as email obviously makes your code less understandable.&lt;/p&gt;&lt;p&gt;The better approach could be passing email to authenticate function like so, but it still has problems:&lt;br&gt;authenticate(self, email=None, password=None)&lt;/p&gt;&lt;p&gt;1) &lt;a href="http://User.email" rel="nofollow noopener" target="_blank" title="User.email"&gt;User.email&lt;/a&gt; doesn't have 'unique' property, which means that your DB won't have index, making your lookups by emails slow like hell.&lt;br&gt;2) You have to deal with username field by completely removing it from your table or altering it to NULL, removing index.&lt;/p&gt;&lt;p&gt;Resuming, both ways are evil and require backend-related code to be executed after syncdb, which is unacceptable in most cases. Seems like the best solutions is just to copy and change auth application.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">dgl</dc:creator><pubDate>Mon, 23 Aug 2010 06:55:45 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-65856990</link><description>&lt;p&gt;As of django 1.2, the username can now accept the characters that are in email addresses.&lt;/p&gt;&lt;p&gt;As for the django developers reasoning, see - &lt;a href="http://groups.google.com/group/django-users/browse_thread/thread/c943ede66e6807c" rel="nofollow noopener" target="_blank" title="http://groups.google.com/group/django-users/browse_thread/thread/c943ede66e6807c"&gt;http://groups.google.com/gr...&lt;/a&gt;&lt;/p&gt;&lt;p&gt;I looked at django-registration. It looks pretty impressive. But the websites I am creating are not open to the public, so it seems like over-kill for my needs.&lt;/p&gt;&lt;p&gt;All I really want to do that is non-standard is allow users to login using their email address as an identifier.&lt;/p&gt;&lt;p&gt;I work w users that are very unsophisticated when it comes to computers. The login process is causes an unbelievable amount of frustration. I am amazed at how many users cannot remember their username. So letting their username be their email address helps quite a bit.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">CharlesM</dc:creator><pubDate>Tue, 03 Aug 2010 11:07:59 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-63394488</link><description>&lt;p&gt;Django-registration make this easy :)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">asinox</dc:creator><pubDate>Tue, 20 Jul 2010 14:37:15 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-38940760</link><description>&lt;p&gt;Have the Django developers given a reason for not wishing to enable Email logins for the built-in User authentication model? Alternatively, have they given a reason for not permitting non-alphanumeric characters in the username field?&lt;/p&gt;&lt;p&gt;Searching for solutions to this issue seems to give a large number of hits for fragmented and 90% working patches or backend extensions. Given that users of Django are obviously experiencing some problems with this developer policy, and the need for Email Usernames is quite apparent, is there a particular reason why they're not willing to implement it?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">tyagi</dc:creator><pubDate>Sun, 20 Dec 2009 00:32:57 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-38940759</link><description>&lt;p&gt;I have the same question as steg.&lt;/p&gt;&lt;p&gt;also this seems backward to me. Why didn't django think "hmm maybe someone might want email as username"&lt;/p&gt;&lt;p&gt;it seems like an ugly hack to attempt to make email unique and can't use built in login form. Is this so hard to fix?&lt;/p&gt;&lt;p&gt;p.s. Please explain the permissions comment...&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">cornbread</dc:creator><pubDate>Thu, 12 Nov 2009 12:22:31 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-38940758</link><description>&lt;p&gt;A possibly naive question:&lt;/p&gt;&lt;p&gt;The EmailOrUsernameModelBackend allows a user to use their email or username during authentication.&lt;br&gt;This backend is then added to the list of AUTHENTICATION_BACKENDS **before** the standard ModelBackend, which performs authentication using the username User field.&lt;/p&gt;&lt;p&gt;My question is why does the EmailOrUsernameModelBackend need to support both email and username, rather than just email? To illustrate my point, if I was to log in with username "steg", the EmailOrUsernameModelBackend.authenticate() method would try to retrieve a User object from the db using keyword arguments {"username":"steg"}. If this fails, django then moves on and tries the ModelBackend.authenticate() method, which will do exactly the same thing.&lt;/p&gt;&lt;p&gt;Are there other reasons for this apparent duplication? I'm pretty new to django, so apologies in advance if I'm missing something obvious.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">steg</dc:creator><pubDate>Sat, 10 Oct 2009 18:33:53 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-38940757</link><description>&lt;p&gt;A user can register the same email address more than once. So I've modified the code a bit to filter by email addresses.&lt;/p&gt;&lt;p&gt;It then loops through each user found and checks the password individually.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">peterp</dc:creator><pubDate>Tue, 14 Jul 2009 05:46:49 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-38940755</link><description>&lt;p&gt;@keredson&lt;br&gt;I use similar approach.&lt;br&gt;I just set the username as a slug of email replacing every weird character with _ and @ with __ &lt;br&gt;Seems to work fine&lt;/p&gt;&lt;p&gt;@David&lt;br&gt;The only problem I have for the moment is the Admin interface to create users instead of allowing them to register...&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">zalun</dc:creator><pubDate>Tue, 30 Jun 2009 19:02:00 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-38940754</link><description>&lt;p&gt;how do you force emails to be unique in django's default admin?  i dropped this code into my test app, and the first thing it gave me was "warning, returned 3 objects not 1!" during "User.objects.get(**kwargs)".&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">keredson</dc:creator><pubDate>Wed, 10 Jun 2009 00:31:59 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-38940753</link><description>&lt;p&gt;Nice, thanks. It pleases me to find out Django allows such elegance&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">markhellewell</dc:creator><pubDate>Tue, 28 Apr 2009 19:09:23 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-3320679</link><description>&lt;p&gt;&amp;gt; The reason it doesn’t inherit from ModelBackend is that this allows you to specify a second backend to use for permissions.&lt;/p&gt;&lt;p&gt;Could you expand on this?  I've been using an EmailBackend similar to yours, except that it inherits from ModelBackend, overrides only the authenticate() method, and I use it all by itself in AUTHENTICATION_BACKENDS.  This seems to work just fine.  What am I missing that your approach provides?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">carljm</dc:creator><pubDate>Thu, 18 Sep 2008 09:26:05 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-3320678</link><description>&lt;p&gt;Hey, just a quick note. If using the standard login views, you won't be able to login with an email that's longer than 30 characters. There's a ticket with a simple patch which hopefully will get checked in to fix that:&lt;br&gt;&lt;a href="http://code.djangoproject.com/ticket/8274" rel="nofollow noopener" target="_blank" title="http://code.djangoproject.com/ticket/8274"&gt;http://code.djangoproject.c...&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Cheers,&lt;/p&gt;&lt;p&gt;Julien Phalip&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">julien</dc:creator><pubDate>Mon, 25 Aug 2008 19:03:41 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-3320677</link><description>&lt;p&gt;Ya I did notice that get_user is still required. I updated the above code.&lt;/p&gt;&lt;p&gt;The reason it doesn't inherit from ModelBackend is that this allows you to specify a second backend to use for permissions.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">David Cramer</dc:creator><pubDate>Mon, 25 Aug 2008 14:03:59 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-3320674</link><description>&lt;p&gt;I just tried the code and realized the backend is missing the other methods that ModelBackend implements so I made EmailOrUsernameModelBackend inherint from ModelBackend instead of object and now everything works like a charm.&lt;/p&gt;&lt;p&gt;The code is here at PasteThat:&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pastethat.com/django_email_login" rel="nofollow noopener" target="_blank" title="http://www.pastethat.com/django_email_login"&gt;http://www.pastethat.com/dj...&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Regards,&lt;/p&gt;&lt;p&gt;Antonio&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">gnrfan</dc:creator><pubDate>Sun, 24 Aug 2008 00:48:54 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-3320676</link><description>&lt;p&gt;Yes, Django is flexible enought to permit this just by adding this small piece of code.&lt;/p&gt;&lt;p&gt;One quirk arises when logging in with an email address in the admin site: if the authentication does not success for any reason you'll get a "Usernames cannot contain the '@' character." error message which is misleading because Django is having no problem to log you in using an email address at this point.&lt;/p&gt;&lt;p&gt;So to correct that you should override the login() method of the admin object as pointed out by James Bennet in this bug:&lt;/p&gt;&lt;p&gt;&lt;a href="http://code.djangoproject.com/ticket/8342" rel="nofollow noopener" target="_blank" title="http://code.djangoproject.com/ticket/8342"&gt;http://code.djangoproject.c...&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Antonio,&lt;br&gt;Lima - Peru&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">gnrfan</dc:creator><pubDate>Sun, 24 Aug 2008 00:25:58 -0000</pubDate></item><item><title>Re: Logging In With Email Addresses in Django</title><link>http://cramer.io//224/logging-in-with-email-addresses-in-django.html#comment-3320675</link><description>&lt;p&gt;wow, it's the piece of code i'm trying to get.&lt;br&gt;thanks for posting it, again!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">sean</dc:creator><pubDate>Sat, 23 Aug 2008 21:46:14 -0000</pubDate></item></channel></rss>