保留自定义 Sign_up 类并在同一 forms.py 文件中导入 allauth 表单会导致导入错误

Roh*_*t.M 1 forms django import django-allauth

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)
Run Code Online (Sandbox Code Playgroud)

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

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

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

文件“/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/allauth/urls.py”,第 8 行,位于 urlpatterns = [url('^', include('allauth.account.urls) '))] 文件“/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/django/conf/urls/ init .py”,第 52 行,包含 urlconf_module = import_module(urlconf_module) 文件“ /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/ init .py”,第 37 行,在 import_module 导入(名称)文件“/Users/rmahamuni/.virtualenvs/todo/lib /python2.7/site-packages/allauth/account/urls.py”,第 4 行,来自 . 导入视图文件“/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/allauth/account/views.py”,第 19 行,来自 .forms import ( 文件“/Users/rmahamuni/. virtualenvs/todo/lib/python2.7/site-packages/allauth/account/forms.py”,第 206 行,在类 BaseSignupForm(_base_signup_form_class()) 中:文件“/Users/rmahamuni/.virtualenvs/todo/lib/python2 .7/site-packages/allauth/account/forms.py",第 188 行,在 _base_signup_form_class ' "%s"' % (fc_module, e)) django.core.exceptions.ImproperlyConfigured:导入表单类 allauth_core.forms 时出错: “无法导入名称 ChangePasswordForm”

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

我尝试切换已安装的应用程序阵容

'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth_core',  <-- app where form is located. 
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?有人可以指导我吗?谢谢

v1k*_*k45 5

发生这种情况是因为尝试导入您在其 .txt 文件allauth中指定为注册表单的给定模块/类。(参见forms.py#L186 @ githubsettings.pyaccount/forms.py

当您通过导入's覆盖其他表单(例如ChangePasswordForm在 settings.py 中)时,会发生循环导入,因为 allauth 已经将您导入到其中allauthaccount/forms.pyforms.pyforms.py

为了避免这种情况,只需将您的单一表单移至单独的表单signupform.py并相应地更改设置即可。它会起作用的。