DJANGO中如何用邮箱来登陆?

就是另一个不同的登陆backend。

而DJANGO会尝不同的方式,哪个成功就用哪个

authentication.py

from django.contrib.auth.models import User


class EmailAuthBackend(object):
    def authenticate(self, username=None, password=None):
        try:
            user = User.objects.get(email=username)
            if user.check_password(password):
                return user
            return None
        except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

  setting.py中加一个认证方式:

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'account.authentication.EmailAuthBackend',
)

  

你可能感兴趣的:(DJANGO中如何用邮箱来登陆?)