使用django-allauth的自定义注册表单运行django时出现错误,该表单是allauth的默认SignupForm的子类。我有
ACCOUNT_SIGNUP_FORM_CLASS = 'rlp.forms.MySignupForm'
settings.py
中的,我的表单代码如下:
class MySignupForm(SignupForm):
'''
Add a "Do you wish to contribute?" option to the default non-social
accounts signup form, and amend the clean method.
'''
wishes_to_contribute = forms.BooleanField(label=_(
'Would you like to request permission to contribute to the '
'platform?'
), widget=forms.CheckboxInput())
#NOTE: this __init__ may be unnecessary
def __init__(self, *args, **kwargs):
super(MySignupForm, self).__init__(*args, **kwargs)
def clean(self):
cleaned_data = super(
MySignupForm, self
).clean()
if cleaned_data['wishes_to_contribute'] == True:
send_mail(
'Contributor request: %s'%cleaned_data['username'],
'New user %s wishes to become a '
'contributor'%cleaned_data['username'],
'from@example.com',
'to@example.com')
return cleaned_data
有一段时间我以为我收到from allauth.account.forms import SignupForm
的导入错误,但如果我在settings.py
中注释掉该行并尝试在django shell中运行from allauth.account.forms import SignupForm
,那么一切正常很好,所以我猜它不是那样的。
错误是:
Traceback (most recent call last):
File "/home/dan/.virtualenvs/platforms/lib/python3.4/site-packages/allauth/account/forms.py", line 192, in _base_signup_form_class
fc_class = getattr(mod, fc_classname)
AttributeError: 'module' object has no attribute 'MySignupForm'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./dan_manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/home/dan/.virtualenvs/platforms/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/dan/.virtualenvs/platforms/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/home/dan/.virtualenvs/platforms/lib/python3.4/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/dan/.virtualenvs/platforms/lib/python3.4/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/home/dan/.virtualenvs/platforms/lib/python3.4/site-packages/django/contrib/admin/apps.py", line 22, in ready
self.module.autodiscover()
File "/home/dan/.virtualenvs/platforms/lib/python3.4/site-packages/django/contrib/admin/__init__.py", line 23, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/home/dan/.virtualenvs/platforms/lib/python3.4/site-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/home/dan/.virtualenvs/platforms/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/dan/Dropbox/devel/platforms/rlp/admin.py", line 4, in <module>
from rlp.forms import EditorAuthenticationForm
File "/home/dan/Dropbox/devel/platforms/rlp/forms.py", line 7, in <module>
from allauth.account.forms import SignupForm
File "/home/dan/.virtualenvs/platforms/lib/python3.4/site-packages/allauth/account/forms.py", line 208, in <module>
class BaseSignupForm(_base_signup_form_class()):
File "/home/dan/.virtualenvs/platforms/lib/python3.4/site-packages/allauth/account/forms.py", line 196, in _base_signup_form_class
fc_classname))
django.core.exceptions.ImproperlyConfigured: Module "rlp.forms" does not define a "MySignupForm" class