【问题标题】:How can I change the "account already exists" message in Django allauth?如何更改 Django allauth 中的“帐户已存在”消息?
【发布时间】:2014-12-17 06:58:37
【问题描述】:

当尝试使用社交帐户登录而该电子邮件已存在帐户时,将显示以下消息:

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 

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

【问题讨论】:

    标签: django django-allauth


    【解决方案1】:

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

    我们来看看forms.pyhttps://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py的代码。我们可以看到raise_duplicate_email_errorBaseSignupForm 的一个方法(并且被它的clean_email 方法调用)。

    因此,您需要从 allauth.account.forms.BaseSignupFormallauth.account.forms.SignupForm 继承(它们也从 BaseSignupForm 继承并添加更多字段)。

    OP 评论后更新BaseSignupForm 派生自 _base_signup_form_class() 函数,它本身导入在 SIGNUP_FORM_CLASS setting 中定义的表单类):嗯,你是对的。问题是BaseSignupFormraise_duplicate_email_errorclean_email方法不会通过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

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

    类 CustomSignupForm(allauth.accounts.forms.SignupForm ): def raise_duplicate_email_error(self): # 这里我尝试覆盖方法,但是没有调用 提出forms.ValidationError( _("此电子邮件地址已存在一个帐户。" “请登录该帐户。”))

    c。在您自己的 urls.py 中添加以下 after include('allauth.urls') 以覆盖 account_signup url

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

    【讨论】:

    • 感谢您的回答。然而BaseSignupForm 派生自_base_signup_form_class() 函数,它本身导入SIGNUP_FORM_CLASS 设置中定义的表单类,这将是我上面的表单。所以它会有效地尝试导入自己。
    • 因为这是一个ValidationError,它只有在用户提交带有附加信息的表单后才会弹出。没关系,因为他们应该知道他们已经有一个帐户,但是如果根本不显示表单并且 allauth 只是重定向到登录并显示一条消息“该电子邮件地址的帐户已存在”,那不是更好吗。请先登录该帐户,然后连接您的...帐户。”
    • 或者只是登录用户并自动连接社交帐户。使用 Google 誓言将是安全的,其他人则不确定。它需要手动设置 AUTHENTICATION_BACKEND,因为它是密码登录。
    • ...因为它是无密码登录:user.backend = settings.AUTHENTICATION_BACKENDS[0]login(request, user)
    【解决方案2】:

    创建覆盖 raise_duplicate_email_error 方法的 CustomSignupForm(allauth.accounts.forms.SignupForm ) 后,您可以使用 0.18 版,将以下内容添加到您的设置文件中:

    SOCIALACCOUNT_FORMS = {
        'signup': 'path.to.your.custom.social.signup.form.CustomSignupForm'
    }
    

    现在应该调用新的 raise_duplicate_email_error 方法。

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 2021-03-17
      • 2020-07-27
      相关资源
      最近更新 更多