Python 允许非活动用户通过自定义django后端登录
我有一个自定义的身份验证后端,我正在玩。我想允许非活动用户登录。将Python 允许非活动用户通过自定义django后端登录,python,django,authentication,backend,Python,Django,Authentication,Backend,我有一个自定义的身份验证后端,我正在玩。我想允许非活动用户登录。将supports\u inactive\u user标志设置为true似乎不起作用,即使我可以验证是否返回了user class AuthenticationBackend(ModelBackend): supports_object_permissions = False supports_anonymous_user = True supports_inactive_user = True
supports\u inactive\u user
标志设置为true似乎不起作用,即使我可以验证是否返回了user
class AuthenticationBackend(ModelBackend):
supports_object_permissions = False
supports_anonymous_user = True
supports_inactive_user = True
def authenticate(self, username=None, password=None):
"""
Allow login with email inplace of username
"""
user = None
if username is not None:
username = username.strip()
if email_re.search(username):
try:
user = User.objects.get(email__iexact=username)
except User.DoesNotExist:
pass
if not user:
try:
user = User.objects.get(username__iexact=username)
except User.DoesNotExist:
return None
if user.check_password(password):
return user
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
我正在使用django 1.4。我遗漏了什么?您的用户成功获得身份验证,但当用户处于非活动状态时,会引发
验证错误。您可以重写子类中的clean方法以捕获相应的ValidationError
:
class InactiveAuthenticationForm(AuthenticationForm):
# a bit messy but it should work
def clean(self):
try:
return super(InactiveAuthenticationForm, self).clean()
except ValidationError as e:
if self.cached_user is not None: # user exists but is not active
# behavior that's skipped because of the validation error
self.check_for_test_cookie()
return self.cleaned_data
else:
raise e
但是,考虑到用户的<代码> ISOActudio标志是一个替换实际删除用户的标志。您可能需要重新考虑您对
的使用是否处于活动状态
。如果您希望用户能够在创建帐户后立即登录,那么有更好的方法来实现这一点 你赢了我:)。哈哈,我跑得很快:实现这一目标的更好方法是什么?在谷歌上,我似乎找不到什么好东西subject@JadS我的意思是,如果您希望用户能够登录,最好的方法是只设置is_active=True
@JadS,那么“激活链接”不是激活链接,帐户已经激活。它可能是电子邮件验证,但处于活动状态
不用于验证电子邮件。