我正在尝试允许人们通过提交表单来创建帐户。当前,这只是继续进入登录屏幕,而不是创建实际用户或出现任何错误。当前已启用调试。
我尝试在user_form.is_valid()之后添加else语句,以确保它不是无效的,但正在发生相同的问题。我猜想这是如何保存表单的问题,但是我是Django的新手,因此感谢您的帮助。
视图:
def home(request):
if request.user.is_authenticated:
return redirect('user_dashboard')
else:
user_form = CreateUserForm(request.POST or None)
if request.method == 'POST':
if user_form.is_valid():
user_form.save()
context = {
'user_form': CreateUserForm,
}
return render(request, 'Main/index.html', context)
型号:
class CreateUserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
confirm_password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = User
fields = [
'username',
'email',
'password',
]
def clean_password(self):
password = self.cleaned_data.get("password")
confirm_password = self.cleaned_data.get("confirm_password")
if password != confirm_password:
raise forms.ValidationError(
"password and confirm_password does not match"
)
else:
return password
答案 0 :(得分:0)
您的问题在这里:
context = {
'user_form': CreateUserForm,
}
应该是:
context = {
'user_form': user_form,
}
干杯:)