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 = (...)

Сообщение об ошибке:

  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
0
Simon J 29 Окт 2019 в 21:37

1 ответ

Подозревали, что это связано с проблемой циклического импорта. Немного изменил код, как показано ниже, и проблема исчезла. Два изменения:

  1. SignupForm теперь наследуется от обычных форм.Form вместо allauth.account.forms.BaseSignupForm.

  2. Перемещено «allauth.account.forms import AddEmailForm» из начала файла прямо перед тем, как это было необходимо.

Все же хотелось бы, чтобы специалисты по Python и/или Django-Allauth разъяснили, что именно произошло.

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 = (...)
0
Simon J 30 Окт 2019 в 18:56