保留自定义 sign_up class 并在同一个 forms.py 文件中导入 allauth 表单导致导入错误

Keeping custom sign_up class and importing allauth forms in same forms.py file causing import errors

from django import forms
from allauth.account.forms import (LoginForm, ChangePasswordForm,
                               ResetPasswordForm, SetPasswordForm, ResetPasswordKeyForm)  
from django.contrib.auth import get_user_model  
from crispy_forms.helper import FormHelper  
from crispy_forms.layout import Layout, Div, Submit, HTML, Button, Row, Field  
from crispy_forms.bootstrap import AppendedText, PrependedText, FormActions  
from django.core.urlresolvers import reverse  


class MySignupForm(forms.Form):
    class Meta:
        model = get_user_model()
        fields = ['email', 'first_name', 'last_name']

    def __init__(self, *args, **kwargs):
        super(MySignupForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.fields["email"].widget.input_type = "email"  # ugly hack
        self.helper.form_method = "POST"
        self.helper.form_action = "account_signup"
        self.helper.form_id = "signup_form"
        self.helper.form_class = "signup"
        self.helper.layout = Layout(
            Field('email', placeholder="Enter Email", autofocus=""),
            Field('first_name', placeholder="Enter First Name"),
            Field('last_name', placeholder="Enter Last Name"),
            Field('password1', placeholder="Enter Password"),
            Field('password2', placeholder="Re-enter Password"),
        Submit('sign_up', 'Sign up', css_class="btn-warning"),
        )

    def signup(self, request, user):
        pass


class MyLoginForm(LoginForm):
    remember_me = forms.BooleanField(required=False, initial=False)

    def __init__(self, *args, **kwargs):
        super(MyLoginForm, self).__init__(*args, **kwargs)


class MyPasswordChangeForm(ChangePasswordForm):

    def __init__(self, *args, **kwargs):
        super(MyPasswordChangeForm, self).__init__(*args, **kwargs)

我的 app.forms.py 文件中有这样的结构,我在其中导入内置表单 LoginForm ResetPasswordForm 等的 allauth,并在同一个文件中定义自定义注册 class.

自定义注册挂钩 class: ACCOUNT_SIGNUP_FORM_CLASS = 'allauth_core.forms.MySignupForm'

我想我遇到了循环导入问题但不确定为什么?

File "/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/allauth/urls.py", line 8, in urlpatterns = [url('^', include('allauth.account.urls'))] File "/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/django/conf/urls/init.py", line 52, in include urlconf_module = import_module(urlconf_module) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/init.py", line 37, in import_module import(name) File "/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/allauth/account/urls.py", line 4, in from . import views File "/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/allauth/account/views.py", line 19, in from .forms import ( File "/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/allauth/account/forms.py", line 206, in class BaseSignupForm(_base_signup_form_class()): File "/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/allauth/account/forms.py", line 188, in _base_signup_form_class ' "%s"' % (fc_module, e)) django.core.exceptions.ImproperlyConfigured: Error importing form class allauth_core.forms: "cannot import name ChangePasswordForm"

如果我将自定义注册表单保存在单独的文件中,那么我不会遇到这个问题。

我尝试切换已安装的应用程序列表

'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth_core',  <-- app where form is located. 

我在这里错过了什么?有人可以指导我吗?谢谢

发生这种情况是因为 allauth 试图在其 account/forms.py 中导入您在 settings.py 中指定为注册表单的给定 module/class。 (参见 forms.py#L186 @ github

当您通过导入 allauthaccount/forms.py 覆盖 ChangePasswordForm 等其他形式时,会发生循环导入,因为 allauth 已经导入了您的 [=16] =] 在其 forms.py

为避免这种情况,只需将您的 singup 表单移至单独的 signupform.py 并相应地更改设置。会成功的。