django-allauth:模块“accounts.forms”没有定义“SignupForm”类

时间:2013-12-26 12:27:34

标签: django django-allauth

我收到以下错误:

  

django.core.exceptions.ImproperlyConfigured:模块“accounts.forms”   没有定义“SignupForm”类

settings.py

(...)

ACCOUNT_SIGNUP_FORM_CLASS = 'accounts.forms.SignupForm'

(...)

帐户/ forms.py

from allauth.account.forms import BaseSignupForm

class SignupForm(BaseSignupForm):

    def __init__(self, *args, **kwargs):
        self.sociallogin = kwargs.pop('sociallogin')
        user = self.sociallogin.account.user
        first_name = forms.CharField(label=_('First name'),
                                     max_length=30,
                                     min_length=2,
                                     widget=forms.TextInput(attrs={
                                         'placeholder':_('First name')}))
        last_name = forms.CharField(label=_('Last name'),
                                     max_length=30,
                                     min_length=2,
                                     widget=forms.TextInput(attrs={
                                         'placeholder':_('Last name')}))
        second_last_name = forms.CharField(label=_('Second last name'),
                                     max_length=30,
                                     empty='',
                                     widget=forms.TextInput(attrs={
                                         'placeholder':_('Second last name')}))
        # TODO: Should become more generic, not listing
        # a few fixed properties.
        initial = {'email': user_email(user) or '',
                   'username': user_username(user) or '',
                   'first_name': user_field(user, 'first_name') or '',
                   'last_name': user_field(user, 'last_name') or ''}
        kwargs.update({
            'initial': initial,
            'email_required': kwargs.get('email_required',
                                         app_settings.EMAIL_REQUIRED)})
        super(SignupForm, self).__init__(*args, **kwargs)

    def save(self, request):
        adapter = get_adapter()
        user = adapter.save_user(request, self.sociallogin, form=self)
        # TODO: Add request?
        super(SignupForm, self).save(user)
        return user

    def raise_duplicate_email_error(self):
        raise forms.ValidationError(
            _("An account already exists with this e-mail address."
              " Please sign in to that account first, then connect"
              " your %s account.")
            % self.sociallogin.account.get_provider().name)

4 个答案:

答案 0 :(得分:18)

先生,您是循环导入的受害者。 allauth尝试从accounts.forms导入您的自定义注册表单类,但是在您从allauth from allauth.account.forms import BaseSignupForm导入的同一文件中。您无需从SignupForm延长BaseSignupForm。只需创建一个简单的表单,allauth就会自动为您扩展它。

答案 1 :(得分:5)

只需继承forms.Form并添加注册功能。

class CustomSignupForm(forms.Form):
    def signup(self, request, user):
        pass

ACCOUNT_SIGNUP_FORM_CLASS = 'app.forms.CustomSignupForm'

答案 2 :(得分:0)

我遇到了这个问题,但不是在SignupForm类中发生的(我按照@aamir的建议正确地引用了它)。

相反,这是因为我将LoginForm子类化为与SignupForm类相同的文件。无论出于何种原因,这也导致了循环进口。只是在我在编写测试时导入视图时才发生这种情况,不知道为什么。

found this solution只是给SignupForm自己的Forms.py文件,以解决此问题。

答案 3 :(得分:-1)

尝试从django-allauth导入SignupForm而不是BaseSignupForm。