Keeping custom sign_up class and importing allauth

2019-03-01 11:04发布

问题:

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)

I have structure like this in my app.forms.py file, where i am importing allauth built in forms LoginForm ResetPasswordForm etc. and in the same file i am defining custom signup class.

hook for custom signup class: ACCOUNT_SIGNUP_FORM_CLASS = 'allauth_core.forms.MySignupForm'

I think i am hitting circular import issue but not sure why ?

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"

If i keep custom signup form in separate file then i dont get this issue.

I tried switching installed apps lineup

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

What am i missing here ? can someone please guide me ? Thanks

回答1:

This happens because allauth tries to import the given module/class you specified as signup form in your settings.py in its account/forms.py. (See forms.py#L186 @ github)

When you override other forms like ChangePasswordForm in your settings.py by importing allauth's account/forms.py, circular import happens because allauth has already imported your forms.py in its forms.py

To avoid this, simply move your singup form to a separate signupform.py and change the settings accordingly. It'll work.