Django allauth创建自定义注册表
我想创建一个带有附加字段的注册表单。我用的是django allauth 当我尝试以普通用户身份注册时,会出现以下错误:Django allauth创建自定义注册表,django,django-forms,django-allauth,Django,Django Forms,Django Allauth,我想创建一个带有附加字段的注册表单。我用的是django allauth 当我尝试以普通用户身份注册时,会出现以下错误: [01/Oct/2020 12:28:15] "GET /accounts/signup/ HTTP/1.1" 200 438 Internal Server Error: /accounts/signup/ Traceback (most recent call last): File "/Users/home/Library/Python
[01/Oct/2020 12:28:15] "GET /accounts/signup/ HTTP/1.1" 200 438
Internal Server Error: /accounts/signup/
Traceback (most recent call last):
File "/Users/home/Library/Python/3.7/lib/python/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Users/home/Library/Python/3.7/lib/python/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/home/Library/Python/3.7/lib/python/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/home/Library/Python/3.7/lib/python/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/Users/home/Library/Python/3.7/lib/python/site-packages/django/views/decorators/debug.py", line 89, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/Users/home/Library/Python/3.7/lib/python/site-packages/allauth/account/views.py", line 215, in dispatch
return super(SignupView, self).dispatch(request, *args, **kwargs)
File "/Users/home/Library/Python/3.7/lib/python/site-packages/allauth/account/views.py", line 81, in dispatch
**kwargs)
File "/Users/home/Library/Python/3.7/lib/python/site-packages/allauth/account/views.py", line 193, in dispatch
**kwargs)
File "/Users/home/Library/Python/3.7/lib/python/site-packages/django/views/generic/base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "/Users/home/Library/Python/3.7/lib/python/site-packages/allauth/account/views.py", line 104, in post
response = self.form_valid(form)
File "/Users/home/Library/Python/3.7/lib/python/site-packages/allauth/account/views.py", line 231, in form_valid
self.user = form.save(self.request)
AttributeError: 'CustomSignupForm' object has no attribute 'save'
[01/Oct/2020 12:28:24] "POST /accounts/signup/ HTTP/1.1" 500 13665
我可以创建超级用户。创建超级用户时,它需要电子邮件,但登录时,它需要用户名。但我可以输入电子邮件作为用户名并成功登录
py:(从中获取此片段)
settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend",
)
SITE_ID = 1
ACCOUNT_EMAIL_VERIFICATION = 'none'
SOCIALACCOUNT_PROVIDERS = {
'google': {
'SCOPE': [
'profile',
'email',
],
'AUTH_PARAMS': {
'access_type': 'online',
}
}
}
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_SESSION_REMEMBER = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False
LOGIN_REDIRECT_URL = 'profile-messages'
ACCOUNT_LOGOUT_REDIRECT_URL = 'homepage'
ACCOUNT_LOGOUT_ON_GET = True
ACCOUNT_SESSION_REMEMBER = True
ACCOUNT_FORMS = {'signup': 'core.forms.CustomSignupForm'}
我找到了解决办法。我目前的代码是:
from allauth.account.forms import SignupForm
from django import forms
class CustomSignupForm(SignupForm):
first_name = forms.CharField(max_length=30, label='First Name')
last_name = forms.CharField(max_length=30, label='Last Name')
def signup(self, request, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
return user
此设置正确吗
ACCOUNT\u FORMS={'signup':'core.FORMS.CustomSignupForm'}
?您的表单所在的位置是否有一个名为core
的应用程序?@Ralf是的,它是正确的
from allauth.account.forms import SignupForm
from django import forms
class CustomSignupForm(SignupForm):
first_name = forms.CharField(max_length=30, label='First Name')
last_name = forms.CharField(max_length=30, label='Last Name')
def signup(self, request, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
return user