使用UserCreationForm问题创建Django用户

根据youtube教程,使用完全相同的方法创建用户,但是我的方法不起作用。为什么?

views.py

from django.shortcuts import render,redirect
from .forms import CreateUserForm

def registerView(request):
    form=CreateUserForm()

    if request.method=="POST":
        form=CreateUserForm(request.POST)
        if form.is_valid():
            form.save()
            user=form.cleaned_data.get('username')
            messages.success(request,'Account was successfully created for '+ user)
            return redirect('login')

    context={'form':form}
    return render(request,"accounts/register.html", context)

表格

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

class CreateUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username','email','password1','password2']

为什么不创建用户?

评论