The question is are you using the right User
model when you defining it in your Form Meta's model attribute:
from .models import User
class CreateUserForm(forms.ModelForm):
class Meta:
model = User
widgets = {
'first_name': TextInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'First Name'}),
'last_name': TextInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Last Name'}),
'email': EmailInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Email Address'}),
'password1': PasswordInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Password'}),
'password2': PasswordInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Repeat Password'}),
}
fields = '__all__'
To be honest, it is best to use Django's built-in User
model or customized version of the User model. If you use Django's built-in model, then you can simply use UserCreationForm
, which provides user creation, password validation, password hashing etc.
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model
class CreateUserForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = get_user_model()
widgets = {
'first_name': TextInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'First Name'}),
'last_name': TextInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Last Name'}),
'email': EmailInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Email Address'}),
'password1': PasswordInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Password'}),
'password2': PasswordInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Repeat Password'}),
}
fields = ['first_name', 'last_name', 'email']
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…