- Author:
- benjaoming
- Posted:
- January 4, 2016
- Language:
- Python
- Version:
- 1.7
- Score:
- 2 (after 2 ratings)
Fixed minimal version, works with Django 1.7+, tested on Django 1.9.
Add the following to your settings:
AUTHENTICATION_BACKENDS = [
'project.backends.UserModelEmailBackend', # Login w/ email
'django.contrib.auth.backends.ModelBackend', # Login w/ username
]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from django.contrib.auth.hashers import check_password
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
class UserModelEmailBackend(ModelBackend):
def authenticate(self, username="", password="", **kwargs):
try:
user = get_user_model().objects.get(email__iexact=username)
if check_password(password, user.password):
return user
else:
return None
except get_user_model().DoesNotExist:
# No user was found, return None - triggers default login failed
return None
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.