这是我来自Allauth的自定义登录表单
class CustomLoginForm(LoginForm):
def __init__(self,*args,**kwargs):
super(CustomLoginForm,self).__init__(*args,**kwargs)
self.fields['login'].widget.attrs['class'] = 'form-control'
self.fields['password'].widget.attrs['class'] = 'form-control'
def login(self, *args, **kwargs):
return super(CustomLoginForm, self).login(*args, **kwargs)
当我仅使用“电子邮件”和“密码”,并且要检查其他选项时,例如上面的custom_field为true时,以上方法效果很好
我看到了这个参考,https://django-allauth.readthedocs.io/en/latest/forms.html#login-allauth-account-forms-loginform,它说添加了您的处理,但是我对如何做到这一点感到震惊。 。 有任何想法吗?
这是一个Form
。您可以像为其他任何Django表单一样向其添加任何字段。并且您可以覆盖clean_my_field()
orclean()
方法来检查该字段的值ValidationError
是否正确,并像其他任何形式一样引发一个不正确的值。
该login()
方法仅用于实际登录用户,如果要在用户登录时执行一些特殊操作,则只需要覆盖它即可。仅当表单有效时才调用此方法。因此,您根本不需要重写此方法。
如果您实际上不是在表单中添加字段,而是仅在对用户进行身份验证时需要在用户上添加检查,那么您不应在表单中进行此操作,而应在身份验证后端中进行此操作。覆盖allauth身份验证后端,以在对用户进行身份验证时添加检查:
from allauth.account.auth_backends import AuthenticationBackend
class MyAuthenticationBackend(AuthenticationBackend):
def authenticate(self, request, **credentials):
user = super().authenticate(request, **credentials)
if user.custom_field == True:
return user
else:
return None
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句