如何更改“帐户已存在”? Django allauth中的消息?

问题描述:

在尝试使用某个社交帐户登录但该电子邮件已有一个帐户时,显示以下消息:

When trying to log in with a social account while there already being an account with that email, the following message shows:

一个帐户该电子邮件地址已经存在。请先登录该帐户,然后再连接您的Google帐户。

现在,我想更改该消息。最初,我尝试覆盖 ACCOUNT_SIGNUP_FORM_CLASS ='mymodule.forms.MySignupForm'并为其赋予自己的 raise_duplicate_email_error 方法,但是

Now I would like to change that message. At first I tried to override ACCOUNT_SIGNUP_FORM_CLASS = 'mymodule.forms.MySignupForm' and gave it my own raise_duplicate_email_error method, but that method is never called.

表单如下:

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 

所以问题是:如何更改该消息?

So the question is: How can I change that message?

如果您想要 raise_duplicate_email_error 要调用的方法,您必须从实际调用 self.raise_duplicate_email_error()的表单类中继承!但是,您只是继承自 forms.Form

If you want the raise_duplicate_email_error method to be called you must inherit from a form class that actually calls self.raise_duplicate_email_error()! However you are just inheriting from forms.Form!

让我们看一下 forms.py ,位于 https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py 。我们可以看到 raise_duplicate_email_error BaseSignupForm 的方法(从其 clean_email 方法)。

Let's take a look at the code of forms.py at https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py. We can see that raise_duplicate_email_error is a method of BaseSignupForm (and is called from its clean_email method).

所以您需要从 allauth.account.forms.BaseSignupForm 继承>或 allauth.account.forms.SignupForm (也继承自 BaseSignupForm ,并向其中添加更多字段)。

So you need to inherit from either allauth.account.forms.BaseSignupForm or allauth.account.forms.SignupForm (which also inherits from BaseSignupForm and adds some more fieldds to it).

在OP发表评论后进行更新 BaseSignupForm 源自 _base_signup_form_class()函数,该函数本身会导入 SIGNUP_FORM_CLASS设置)中定义的表单类:嗯,您是对的。问题是 BaseSignupForm raise_duplicate_email_error clean_email 方法请勿通过super调用其祖先的同名方法(因此永远不会调用您的 raise_duplicate_email_error )。

Update after OP's comment (that the BaseSignupForm is derived from the _base_signup_form_class() function, that itself imports the form class defined in the SIGNUP_FORM_CLASS setting ): Hmm you are right. The problem is that the raise_duplicate_email_error and clean_email methods of BaseSignupForm don't call the same-named methods of their ancestors through super (so your raise_duplicate_email_error is never called).

让我们看看视图的作用:如果您在网址中添加了 url(r'^ accounts /',include('allauth.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

Let's see what the view does: If you have added the line url(r'^accounts/', include('allauth.urls')), to your urls.py (which is the usual thing to do for django-allauth), you'll see the line url(r"^signup/$", views.signup, name="account_signup"), in the file https://github.com/pennersr/django-allauth/blob/13edcfef0d7e8f0de0003d6bcce7ef58119a5945/allauth/account/urls.py and then in the file https://github.com/pennersr/django-allauth/blob/13edcfef0d7e8f0de0003d6bcce7ef58119a5945/allauth/account/views.py you'll see the definition of signup as signup = SignupView.as_view(). So let's override SignupView to use our own form and then use our class view for the account_sigunp !

操作方法如下:

a。创建继承 SignupView 并覆盖表单类的自定义视图

a. Create your custom view that inherits SignupView and overrides the form class


class CustomFormSignupView(allauth.accounts.views.SignupView):
    form_class = CustomSignupForm

b。创建一个自定义表单,该表单继承自 SignupForm 并覆盖电子邮件验证消息

b. Create a custom form that inherits from SignupForm and overrides the email validation message


class CustomSignupForm(allauth.accounts.forms.SignupForm ):
    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."))

c。在您自己的urls.py中,在之后 include('allauth.urls')中添加以下代码,以覆盖 account_signup 网址

c. In your own urls.py add the following after include('allauth.urls') to override the account_signup url


    url(r'^accounts/', include('allauth.urls')),
    url(r"^accounts/signup/$", CustomFormSignupView.as_view(), name="account_signup"),``