Django-allauth 创建自定义注册表单
Django-allauth creating custom registration form
我想创建一个带有附加字段的注册表单。我正在使用 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/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
我可以创建超级用户。创建超级用户时,它需要电子邮件,但在登录时,它需要用户名。但是我可以输入email作为用户名并成功登录。
forms.py:(从 https://github.com/pennersr/django-allauth/issues/1412 中获取此片段)
class CustomSignupForm(forms.Form):
first_name = forms.CharField(max_length=30, label='First name')
last_name = forms.CharField(max_length=30, label='Last name')
email = forms.EmailField(required=True)
password = forms.CharField(widget=forms.PasswordInput)
def signup(self, request, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
user.password = self.cleaned_data['password']
user.save()
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
我想创建一个带有附加字段的注册表单。我正在使用 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/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
我可以创建超级用户。创建超级用户时,它需要电子邮件,但在登录时,它需要用户名。但是我可以输入email作为用户名并成功登录。
forms.py:(从 https://github.com/pennersr/django-allauth/issues/1412 中获取此片段)
class CustomSignupForm(forms.Form):
first_name = forms.CharField(max_length=30, label='First name')
last_name = forms.CharField(max_length=30, label='Last name')
email = forms.EmailField(required=True)
password = forms.CharField(widget=forms.PasswordInput)
def signup(self, request, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
user.password = self.cleaned_data['password']
user.save()
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