django allauth 中的 Django 排序字段
Django Ordering Fields in django allauth
我试图将字段按 'full name'、'email' 和 'password' 的顺序排序,但现在是 'email'、'full_name' 然后 'password'.
Here is the image
我尝试将此添加到我的注册表单中,但没有成功:
if 'keyOrder' in self.fields:
self.fields.keyOrder = ['full_name', 'email', 'password']
这是我的其余文件:
forms.py
from allauth.account.forms import SignupForm
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django import forms
from .models import CustomUser
class CustomSignupForm(SignupForm):
full_name = forms.CharField(max_length=50, label='Full Name')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['full_name'].widget.attrs.update({'autofocus': 'autofocus'})
# Doesn't work
if 'keyOrder' in self.fields:
self.fields.keyOrder = ['full_name', 'email', 'password']
for fieldname in ['password1']:
self.fields[fieldname].help_text = "Your password"
adapters.py
在这里,我将全名保存到 customuser 模型
from allauth.account.adapter import DefaultAccountAdapter
from django import forms
class UserAccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=True):
"""
This is called when saving user via allauth registration.
We override this to set additional data on user object.
"""
user = super(UserAccountAdapter, self).save_user(request, user, form, commit=False)
user.full_name = form.cleaned_data.get('full_name')
user.save()
在settings.py
这些是我在设置文件底部设置的 django all-auth 配置
AUTH_USER_MODEL = 'accounts.CustomUser'
# django-allauth config
LOGIN_REDIRECT_URL = 'home'
ACCOUNT_LOGOUT_REDIRECT = 'home' # new
SITE_ID = 1 # new
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend', # new
)
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' # new
ACCOUNT_SESSION_REMEMBER = True # new
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False # new
ACCOUNT_USERNAME_REQUIRED = False # new
ACCOUNT_AUTHENTICATION_METHOD = 'email' # new
ACCOUNT_EMAIL_REQUIRED = True # new
ACCOUNT_UNIQUE_EMAIL = True # new
ACCOUNT_FORMS = {
'signup': 'accounts.forms.CustomSignupForm',
}
ACCOUNT_ADAPTER = 'accounts.adapter.UserAccountAdapter'
# ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5
ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 300
ACCOUNT_LOGIN_ON_PASSWORD_RESET = True
models.py
这些是我制作的自定义用户模型。我确保在迁移任何内容之前写下此内容
from django.contrib.auth.models import AbstractUser
from django.db import models
from uuid import uuid4
# Create your models here.
class CustomUser(AbstractUser):
id = models.UUIDField( # new
primary_key=True,
default=uuid4,
editable=False,
max_length=70)
full_name = models.CharField(max_length=75, blank=True)
bio = models.TextField(max_length=250, blank=True)
def __str__(self):
return str(self.full_name)
您可以设置 field_order 属性来覆盖 default_field_order(来自 BaseSignupForm source)
set_form_field_order(
self,
getattr(self, 'field_order', None) or default_field_order)
所以在你的情况下是这样的
class CustomSignupForm(SignupForm):
...
field_order = ['full_name', 'email', 'password']
...
我试图将字段按 'full name'、'email' 和 'password' 的顺序排序,但现在是 'email'、'full_name' 然后 'password'.
Here is the image
我尝试将此添加到我的注册表单中,但没有成功:
if 'keyOrder' in self.fields:
self.fields.keyOrder = ['full_name', 'email', 'password']
这是我的其余文件:
forms.py
from allauth.account.forms import SignupForm
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django import forms
from .models import CustomUser
class CustomSignupForm(SignupForm):
full_name = forms.CharField(max_length=50, label='Full Name')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['full_name'].widget.attrs.update({'autofocus': 'autofocus'})
# Doesn't work
if 'keyOrder' in self.fields:
self.fields.keyOrder = ['full_name', 'email', 'password']
for fieldname in ['password1']:
self.fields[fieldname].help_text = "Your password"
adapters.py
在这里,我将全名保存到 customuser 模型
from allauth.account.adapter import DefaultAccountAdapter
from django import forms
class UserAccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=True):
"""
This is called when saving user via allauth registration.
We override this to set additional data on user object.
"""
user = super(UserAccountAdapter, self).save_user(request, user, form, commit=False)
user.full_name = form.cleaned_data.get('full_name')
user.save()
在settings.py 这些是我在设置文件底部设置的 django all-auth 配置
AUTH_USER_MODEL = 'accounts.CustomUser'
# django-allauth config
LOGIN_REDIRECT_URL = 'home'
ACCOUNT_LOGOUT_REDIRECT = 'home' # new
SITE_ID = 1 # new
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend', # new
)
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' # new
ACCOUNT_SESSION_REMEMBER = True # new
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False # new
ACCOUNT_USERNAME_REQUIRED = False # new
ACCOUNT_AUTHENTICATION_METHOD = 'email' # new
ACCOUNT_EMAIL_REQUIRED = True # new
ACCOUNT_UNIQUE_EMAIL = True # new
ACCOUNT_FORMS = {
'signup': 'accounts.forms.CustomSignupForm',
}
ACCOUNT_ADAPTER = 'accounts.adapter.UserAccountAdapter'
# ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5
ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 300
ACCOUNT_LOGIN_ON_PASSWORD_RESET = True
models.py 这些是我制作的自定义用户模型。我确保在迁移任何内容之前写下此内容
from django.contrib.auth.models import AbstractUser
from django.db import models
from uuid import uuid4
# Create your models here.
class CustomUser(AbstractUser):
id = models.UUIDField( # new
primary_key=True,
default=uuid4,
editable=False,
max_length=70)
full_name = models.CharField(max_length=75, blank=True)
bio = models.TextField(max_length=250, blank=True)
def __str__(self):
return str(self.full_name)
您可以设置 field_order 属性来覆盖 default_field_order(来自 BaseSignupForm source)
set_form_field_order(
self,
getattr(self, 'field_order', None) or default_field_order)
所以在你的情况下是这样的
class CustomSignupForm(SignupForm):
...
field_order = ['full_name', 'email', 'password']
...