0
        aqszzFollowed instructions as on [the page][1]. to customize user profile while using django-allauth. Django/python saying it can't find the "SignupForm" class definition, which is clearly defined in the file forms.py in the users app as in the code below. Anyone has any idea what's going on?

forms.py

from django import forms
from allauth.account.forms import AddEmailForm, BaseSignupForm
from django.core.exceptions import ValidationError
from django.conf import settings
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser

class SignupForm(BaseSignupForm):
    first_name = forms.CharField(max_length=30, label='Firstname')
    last_name = forms.CharField(max_length=150, label='Lastname')

    def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()

class MyAddEmailForm(AddEmailForm):
    def clean_email(self):
        email = super().clean_email()
        if self.user.emailaddress_set.count() >= settings.USERS_EMAILS_MAX_COUNT:
            raise ValidationError('Number of related emails can be no more than %d.' % settings.USERS_EMAILS_MAX_COUNT)
        return email

class CustomUserCreationForm(UserCreationForm):
    class Meta:
        model = CustomUser
        fields = (...)

class CustomUserChangeForm(UserChangeForm):
    class Meta:
        model = CustomUser
        fields = (...)

Error message is:

  File "D:\Python\Django\m4ever\users\admin.py", line 4, in <module>
    from .forms import CustomUserCreationForm, CustomUserChangeForm
  File "D:\Python\Django\m4ever\users\forms.py", line 3, in <module>
    from allauth.account.forms import AddEmailForm, BaseSignupForm
  File "C:\Users\Freedom\Anaconda3\envs\myvenv\lib\site-packages\allauth\account\forms.py", line 261, in <module>
    class BaseSignupForm(_base_signup_form_class()):
  File "C:\Users\Freedom\Anaconda3\envs\myvenv\lib\site-packages\allauth\account\forms.py", line 249, in _base_signup_form_class
    fc_classname))
django.core.exceptions.ImproperlyConfigured: Module "users.forms" does not define a "SignupForm" class
David Buck
  • 3,752
  • 35
  • 31
  • 35
Simon J
  • 21
  • 3

1 Answers1

0

Have been suspecting it's related with circular importing issue. Changed the code a bit to as below, and the issue is gone. The two changes are:

  1. SignupForm now inheriting from normal forms.Form instead of allauth.account.forms.BaseSignupForm

  2. Moved "allauth.account.forms import AddEmailForm" from the beginning of the file, to right before it was needed.

Would still like experts in Python and/or Django-Allauth to clarify exactly what happened.

from django import forms
from django.core.exceptions import ValidationError
from django.conf import settings
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser

class SignupForm(forms.Form):
    first_name = forms.CharField(max_length=30, label='Firstname')
    last_name = forms.CharField(max_length=150, label='Lastname')

    def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()

from allauth.account.forms import AddEmailForm
class MyAddEmailForm(AddEmailForm):
    def clean_email(self):
        email = super().clean_email()
        if self.user.emailaddress_set.count() >= settings.USERS_EMAILS_MAX_COUNT:
            raise ValidationError('Number of related emails can be no more than %d.' % settings.USERS_EMAILS_MAX_COUNT)
        return email

class CustomUserCreationForm(UserCreationForm):
    class Meta:
        model = CustomUser
        fields = (...)

class CustomUserChangeForm(UserChangeForm):
    class Meta:
        model = CustomUser
        fields = (...)
Simon J
  • 21
  • 3