UserCreationForm 不保存密码 commit=False(未设置密码)

UserCreationForm doesn't save the password commit=False (No password set)

我在 HTML 上创建了一个注册表单 ajax 提取数据并通过 views.py 中的函数将其发送到 UserCreationForm。但是当我检查用户时,我意识到密码没有保存。 我正在使用 ajax 所以我获取了每个字段的数据。在视图函数中,我必须一个一个地添加字段值。我使用了 set_password 但没有帮助。

非常感谢任何帮助谢谢 我的表格:

class RegisterForm(UserCreationForm):
    username = forms.CharField(max_length=100 , widget=forms.TextInput(
        attrs={'class': "form-control"}))
    email = forms.EmailField(max_length=100, widget=forms.EmailInput())
        
    password1 = forms.CharField(widget=forms.PasswordInput(
        attrs={'class': "form-control"}))
    password2 = forms.CharField(widget=forms.PasswordInput(
        attrs={'class': "form-control"}))
    
    email.widget.attrs.update({'class': 'form-control'})

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        user.set_password(self.cleaned_data['password1'])
        if commit:
            user.save()
        return user

我的看法:

   def home(request):
    register_form = RegisterForm()
    workshop_form = WorkshopCreateForm
    if request.method == 'POST':
        register_form = RegisterForm(request.POST)
        username = request.POST.get('username')
        print(username)
        email = request.POST.get('email')
        password1 = request.POST.get('pass1')
        password2 = request.POST.get('pass2')
        if register_form.is_valid():
            pended_form = register_form.instance
            pended_form.username = username
            pended_form.email = email
            if password1 == password2:
                pended_form.set_password(password1)
                pended_form.save()
                ctx = {
                    'username': pended_form.username,
                    'password': pended_form.password,
                    'created': True,
                    'success': True,
                    'status':'You created your account',
                }
                return JsonResponse(ctx)
            else:
                ctx = {
                    'username': '',
                    'password': '',
                    'created': False,
                    'success': False,
                    'status':'error',
                    'msg': _('Passwords do not match')
                }
                return JsonResponse(ctx)
                #! TODO: Create a function and import login func from there
        else:
            ctx = {
                    'created': False,
                    'success': False,
                    'status':'error',
                    'msg': _('You need to enter a valid username and email')
                }
            return JsonResponse(ctx)
    return render(request, 'web/home.html', {'register_form':register_form})

用户保存后:

只需创建一个新用户。

User.create.objects(username, email)
user.set_password(password)
user.save()

您可以在您的视图中执行此操作

我需要直接使用 RegisterForm() 而无需解析。我不需要解析数据。我只是把数据放在 RegisterForm() 里面所以问题就解决了。

if request.method == 'POST' and request.POST.get("operation") == "register":
        register_form = RegisterForm(request.POST)
        if register_form.is_valid():
            register_form.save()
            return JsonResponse(ctx)