2014-12-17 5 views
3

이미 이메일의 계정이하면서 사회 계정으로 로그인을 시도, 다음과 같은 메시지가 표시됩니다 :장고 allauth에서 "account already exists"메시지를 어떻게 바꿀 수 있습니까?

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 

그래서 질문입니다

: 어떻게 그 메시지를 변경할 수 있습니다

양식은 다음과 같습니다?

답변

3

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_errorBaseSignupForm이라는 메서드이며 (해당 메서드의 clean_email 메서드에서 호출 됨) 볼 수 있습니다.

따라서 allauth.account.forms.BaseSignupForm 또는 allauth.account.forms.SignupForm (상속자는 BaseSignupForm에서 상속 받고 더 많은 필드를 추가해야 함). 흠 당신은 맞다 :

업데이트합니다 (BaseSignupForm 자체가 SIGNUP_FORM_CLASS setting에 정의 된 형태로 클래스를 가져 것으로는 _base_signup_form_class() 기능에서 파생되는) 영업 이익의 의견 후. 문제는 raise_duplicate_email_errorclean_email 메서드가 BaseSignupForm이므로 super를 통해 조상의 동일한 이름의 메서드를 호출하지 않기 때문에 raise_duplicate_email_error이 호출되지 않습니다.

이의 뷰가 무엇을 보자 : 당신이 (장고 - allauth을 위해 할 수있는 일반적인 일) 당신의 urls.py에 선 url(r'^accounts/', include('allauth.urls')),을 추가 한 경우, 파일의 라인 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의 정의가 signup = SignupView.as_view()으로 표시됩니다. 따라서 SignupView을 재정 의하여 자체 양식을 사용하고 account_sigunp에 대한 수업보기를 사용합시다!

다음은 수행 방법입니다.

a. SignupView을 상속 폼 클래스를

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

B보다 우선 사용자 정의보기를 만듭니다. SignupForm에서 상속 및 이메일 확인 메시지를

 
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에서 버전으로, 당신이 할 수는 raise_duplicate_email_error 방법보다 우선 CustomSignupForm (allauth.accounts.forms.SignupForm)을 생성 한 후 account_signup URL을

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

답변 해 주셔서 감사합니다. 그러나''BaseSignupForm''은''_base_signup_form_class()''함수에서 파생됩니다. 그 자체는''SIGNUP_FORM_CLASS'' 설정에 정의 된 양식 클래스를 가져옵니다. 이것은 위에있는 양식이 될 것입니다. 그래서 그것은 효과적으로 스스로를 수입하려고 시도 할 것입니다. –

+0

이것은 'ValidationError'이므로 사용자가 추가 정보가있는 양식을 제출 한 후에 만 ​​팝업됩니다. 이미 계정이 있지만, 양식이 전혀 표시되지 않고 allauth가 "이 전자 메일 주소가있는 계정이 이미 있습니다"라는 메시지와 함께 로그인으로 리다이렉트 된 경우에는 더 좋지 않을 것입니다. 먼저 해당 계정에 로그인하고 계정을 연결하십시오. " –

+0

또는 사용자를 로그인하고 자동 소셜 계정에 연결하십시오. 이것은 다른 사람들에 대해 확실하지 않은 Google 서약으로 안전합니다. 비밀번호 로그인이므로 AUTHENTICATION_BACKEND를 수동으로 설정해야합니다. –

0

을 무시할include('allauth.urls') 다음에 다음 를 추가 0.18, 설정 파일에 다음을 추가

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

새로운 raise_duplicate_email_error 방법은 이제 호출해야합니다.

희망이 도움이됩니다.

관련 문제