dan*_*old 6 django django-allauth
在已有使用该电子邮件的帐户的情况下尝试使用社交帐户登录时,会显示以下消息:
An account already exists with this e-mail address. Please sign in to that account first, then connect your Google account.
现在我想更改该消息。起初我试图覆盖ACCOUNT_SIGNUP_FORM_CLASS = 'mymodule.forms.MySignupForm'并给它我自己的raise_duplicate_email_error方法,但该方法从未被调用。
表格如下所示:
class SignupForm(forms.Form):
first_name = forms.CharField()
last_name = forms.CharField()
boolflag = forms.BooleanField()
def raise_duplicate_email_error(self):
# here I tried to override the method, but it is not called
raise forms.ValidationError(
_("An account already exists with this e-mail address."
" Please sign in to that account."))
def signup(self, request, user):
# do stuff to the user and save it
Run Code Online (Sandbox Code Playgroud)
所以问题是:我该如何更改该消息?
如果要raise_duplicate_email_error调用该方法,则必须从实际调用的表单类继承self.raise_duplicate_email_error()!但是,您只是继承了forms.Form!
让我们来看看的代码forms.py在https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py。我们可以看到这raise_duplicate_email_error是一个方法BaseSignupForm(并从它的clean_email方法中调用)。
所以你需要从allauth.account.forms.BaseSignupFormorallauth.account.forms.SignupForm继承(它也继承BaseSignupForm并添加了更多的字段)。
在 OP 的评论之后更新(BaseSignupForm从_base_signup_form_class()函数派生,它本身导入了在 中定义的表单类SIGNUP_FORM_CLASS setting):嗯,你是对的。问题是 的raise_duplicate_email_error和clean_email方法BaseSignupForm不会通过 super 调用其祖先的同名方法(因此raise_duplicate_email_error永远不会调用您的方法)。
让我们看看视图做了什么:如果您已将该行添加url(r'^accounts/', include('allauth.urls')),到您的 urls.py(这是 django-allauth 通常要做的事情),您将url(r"^signup/$", views.signup, name="account_signup"),在文件https://github.com/pennersr 中看到该行/django-allauth/blob/13edcfef0d7e8f0de0003d6bcce7ef58119a5945/allauth/account/urls.py,然后在文件中
https://github.com/pennersr/django-allauth/blob/13edcfef0d7e8f0de0003d6bcce7ef58119a5945/allauth/account/views.py你会看到注册的定义为signup = SignupView.as_view()。因此,让我们重写SignupView以使用我们自己的表单,然后使用我们的类视图来处理account_sigunp!
这是如何做到的:
一种。创建继承SignupView和覆盖表单类的自定义视图
类 CustomFormSignupView(allauth.accounts.views.SignupView):
form_class = CustomSignupForm
湾 创建一个继承SignupForm并覆盖电子邮件验证消息的自定义表单
类 CustomSignupForm(allauth.accounts.forms.SignupForm):
def raise_duplicate_email_error(self):
# 在这里我试图覆盖该方法,但它没有被调用
引发 forms.ValidationError(
_("此电子邮件地址已存在帐户。"
"请登录该帐户。"))
C。在你自己的urls.py添加以下后 include('allauth.urls')覆盖account_signup网址
url(r'^accounts/', include('allauth.urls')),
url(r"^accounts/signup/$", CustomFormSignupView.as_view(), name="account_signup"),``
| 归档时间: |
|
| 查看次数: |
1501 次 |
| 最近记录: |