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
!
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).
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).
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).
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
!
Here's how to do it:
a. Create your custom view that inherits SignupView
and overrides the form class
class CustomFormSignupView(allauth.accounts.views.SignupView):
form_class = CustomSignupForm
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. 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"),``