Use email addresses for user name


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. from django.contrib.auth.models import User
  2. from django.core.validators import email_re
  3.  
  4. class BasicBackend:
  5. def get_user(self, user_id):
  6. try:
  7. return User.objects.get(pk=user_id)
  8. except User.DoesNotExist:
  9. return None
  10.  
  11. class EmailBackend(BasicBackend):
  12. def authenticate(self, username=None, password=None):
  13. #If username is an email address, then try to pull it up
  14. if email_re.search(username):
  15. try:
  16. user = User.objects.get(email=username)
  17. except User.DoesNotExist:
  18. return None
  19. else:
  20. #We have a non-email address username we should try username
  21. try:
  22. user = User.objects.get(username=username)
  23. except User.DoesNotExist:
  24. return None
  25. if user.check_password(password):
  26. return user

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.