保持自定义sign_up类并在相同的forms.py文件中导入allauth表单导致导入错误

时间:2016-03-11 16:56:12

标签: django forms 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)

我的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行,in       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)     在import_module中输入文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/init.py”,第37行       导入(名称)     文件“/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/allauth/account/urls.py”,第4行,in       来自。导入视图     文件“/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/allauth/account/views.py”,第19行,in       from .forms import(     文件“/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/allauth/account/forms.py”,第206行,       class BaseSignupForm(_base_signup_form_class()):     在_base_signup_form_class中输入文件“/Users/rmahamuni/.virtualenvs/todo/lib/python2.7/site-packages/allauth/account/forms.py”,第188行       '“%s”'%(fc_module,e))   django.core.exceptions.ImproperlyConfigured:导入表单类allauth_core.forms时出错:“无法导入名称ChangePasswordForm”

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

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

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

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

1 个答案:

答案 0 :(得分:3)

这是因为allauth尝试导入您在其settings.py account/forms.py中指定为注册表单的给定模块/类。 (见forms.py#L186 @ github

当您通过导入ChangePasswordForm的{​​{1}}覆盖settings.py中的allauth等其他表单时,会发生循环导入,因为allauth已经在其中导入了account/forms.py forms.py

要避免这种情况,只需将您的单身表格移至单独的forms.py并相应更改设置即可。它会工作。