2017-03-22 1 views
0

내 가입 양식을 사용자 정의하기 위해 django allauth의 SignupForm을 확장하려고합니다. 그 이유는 제출 된 이메일이 가입 여부를 확인하고 싶기 때문이며 그 이메일의 초대가 관리자가 수락했는지 여부를 확인하여 완료됩니다.django.core.exceptions.ImproperlyConfigured : 양식 클래스를 가져 오는 중 오류가 발생했습니다. pages.custom_sign_up_form : "BaseSignupForm '을 가져올 수 없습니다.

나는 form.py에서 내 양식을 정의하지 않았으며 대신 'custom_sign_up_form.py'라는 다른 이름을 사용했습니다. 아래는 가져온 전체 코드입니다.

settings.py

ACCOUNT_SIGNUP_FORM_CLASS = 'pages.custom_sign_up_form.CustomSignupForm' 

custom_sign_up_form.py

from allauth.account.adapter import DefaultAccountAdapter, get_adapter 
from allauth.account.forms import SignupForm 
from allauth.account import app_settings 
# from django import forms 

from invitation.models import Invitation 

class CustomSignupForm(SignupForm): 
    errors = { 
     'not_invited': "Sorry! You are not yet invited.", 
    } 
    def __init__(self, *args, **kwargs): 
     super(CustomSignupForm, self).__init__(*args, **kwargs) 

    def clean(self): 
     super(CustomSignupForm, self).clean() 
     email = self.cleaned_data.get('email') 
     email = get_adapter().clean_email(email) 
     if email and app_settings.UNIQUE_EMAIL: 
      email = self.validate_unique_email(email) 
     try: 
      Invitation.objects.get(email=email, request_approved=True) 
     except Invitation.DoesNotExist: 
      # raise forms.ValidationError(errors['Sorry! you are not yet invited']) 
      self.add_error('email','Sorry! You are not yet invited') 
     return email 
+0

이것이 동일한 참조 qustion이다? https://github.com/pennersr/django-allauth/issues/572 –

+0

그는 SignupForm을 확장하는 대신 forms.Form을 사용한다고 말합니다. 이제 폼을 확장해야합니다. 폼 클래스. 그러나 나는 어디에 내 검증 논리를 작성해야하는지 혼란 스럽다. 가입 또는 청소 기능? – pythonBeginner

+0

나는 어떻게하는지 이해함에 따라 귀하의 질문에 대한 답변을 얻었습니다. –

답변

0

ACCOUNT_SIGNUP_FORM_CLASS 상위 클래스의 SignupForm (BaseSignupForm) 인 ...

https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py#L197

양식 유효성 검사 논리를 변경하려면 ACCOUNT_ADAPTER을 사용해야합니다.

# project/settings.py: 
ACCOUNT_ADAPTER = 'project.users.adapter.MyAccountAdapter' 

# project/users/adapter.py: 
from django.conf import settings 
from allauth.account.adapter import DefaultAccountAdapter 

class MyAccountAdapter(DefaultAccountAdapter): 

    def clean_email(self, email): 
     """ 
     Validates an email value. You can hook into this if you want to 
     (dynamically) restrict what email addresses can be chosen. 
     """ 
     try: 
      Invitation.objects.get(email=email, request_approved=True) 
     except Invitation.DoesNotExist: 
      raise forms.ValidationError('Sorry! you are not yet invited') 
     return email 

또한 https://stackoverflow.com/a/23328414/7724457

관련 문제