Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#in your forms
#if you are using django.contrib.auth.views.login
#pass EmailAuthenticationForm to the authentication_form argument.
class EmailAuthenticationForm(AuthenticationForm):
Extends the standard django AuthenticationForm, to support 75 character
usernames. 75 character usernames are needed to support the EmailOrUsername
auth backend.
required_css_class = 'required'
username = forms.CharField(label=_('Username'), max_length=75)
password = forms.CharField(label=_('Password'), widget=forms.PasswordInput)
def __init__(self, request=None, *args, **kwargs):
self.request = request
self.user_cache = None
super(AuthenticationForm, self).__init__(*args, **kwargs)
from django.core.validators import email_re
class EmailOrUsernameModelBackend(ModelBackend):
"""Allows a user to login using their email address, and not just their
def _lookup_user(self, username):
user = User.objects.get(email=username.lower())
user = User.objects.get(username=username)
except User.DoesNotExist:
return None
return user
def authenticate(self, username=None, password=None):
user = self._lookup_user(username)
if user:
if user.check_password(password):
return user
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment