一尘不染

在Django中使用电子邮件地址或用户名登录用户

django

我正在尝试创建一个auth后端,以允许我的用户使用他们的电子邮件地址或他们在Django 1.6中使用自定义用户模型的用户名登录。当我使用用户名登录时,后端工作正常,但由于某种原因,没有电子邮件。有什么我忘记要做的事吗?

from django.conf import settings
from django.contrib.auth.models import User

class EmailOrUsernameModelBackend(object):
    """
    This is a ModelBacked that allows authentication with either a username or an email address.

    """
    def authenticate(self, username=None, password=None):
        if '@' in username:
            kwargs = {'email': username}
        else:
            kwargs = {'username': username}
        try:
            user = User.objects.get(**kwargs)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

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

编辑:按照建议,我已从ModelBackend继承并将其安装在我的设置中。在我的设置中,我有这个AUTHENTICATION_BACKENDS ='users.backends','django.contrib.auth.backends.ModelBackend',)并且我将后端更改为这个:

from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.backends import ModelBackend
class EmailOrUsernameModelBackend(ModelBackend):
    """
    This is a ModelBacked that allows authentication with either a username or an email address.

    """
    def authenticate(self, username=None, password=None):
        if '@' in username:
            kwargs = {'email': username}
        else:
            kwargs = {'username': username}
        try:
            user = User.objects.get(**kwargs)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

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

现在我得到一个Module "users" does not define a "backends" attribute/class错误。


阅读 390

收藏
2020-04-02

共1个答案

一尘不染

遵循上面给我的建议并进行更改后,AUTHENTICATION_BACKENDS = ['yourapp.yourfile.EmailOrUsernameModelBackend']我遇到了错误Manager isn't available; User has been swapped for 'users.User'。这是由于我使用的是默认用户模型,而不是我自己的自定义模型。这是工作代码。

from django.conf import settings
from django.contrib.auth import get_user_model

class EmailOrUsernameModelBackend(object):
    """
    This is a ModelBacked that allows authentication with either a username or an email address.

    """
    def authenticate(self, username=None, password=None):
        if '@' in username:
            kwargs = {'email': username}
        else:
            kwargs = {'username': username}
        try:
            user = get_user_model().objects.get(**kwargs)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

    def get_user(self, username):
        try:
            return get_user_model().objects.get(pk=username)
        except get_user_model().DoesNotExist:
            return None
2020-04-02