gpt4 book ai didi

django-allauth - Django-Allauth 中的自定义表单验证

转载 作者:行者123 更新时间:2023-12-04 12:08:05 26 4
gpt4 key购买 nike

我想对 django-allauth 中的字段进行一些额外的验证。例如,我想阻止使用免费电子邮件地址。所以我想在注册时运行这个方法

def clean_email(self):
email_domain = self.cleaned_data['email'].split('@')[1]
if email_domain in self.bad_domains:
raise forms.ValidationError(_("Registration using free email addresses is prohibited. Please supply a different email address."))

同样,我想对电子邮件地址以外的不同字段运行自定义验证。我该如何执行此操作?

最佳答案

allauth 配置上有一些适配器。例如这个:

ACCOUNT_ADAPTER (="allauth.account.adapter.DefaultAccountAdapter")
Specifies the adapter class to use, allowing you to alter certain default behaviour.

您可以通过覆盖默认适配器来指定新适配器。只需覆盖 clean_email 方法。
class MyCoolAdapter(DefaultAccountAdapter):

def clean_email(self, email):
"""
Validates an email value. You can hook into this if you want to
(dynamically) restrict what email addresses can be chosen.
"""
*** here goes your code ***
return email

然后修改settings.py上的ACCOUNT_ADAPTER
ACCOUNT_ADAPTER = '**app**.MyCoolAdapter'

检查默认行为:
https://github.com/pennersr/django-allauth/blob/master/allauth/account/adapter.py

关于django-allauth - Django-Allauth 中的自定义表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23304588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com