Django Form在GET方法上显示表单错误

iyfjxgzm  于 2023-03-31  发布在  Go
关注(0)|答案(2)|浏览(135)

我使用django web framework已经有一段时间了,我想写一个自定义的密码更改表单,而不使用django或第三方软件包提供的默认密码。
我尝试过遵循django SetPasswordForm和PasswordChangeForm中使用的类似模式

我的当前实现

项目/应用程序/forms.py

class ChangePasswordForm(forms.Form):
    old_password = forms.CharField(max_length=50, )
    password1 = forms.CharField(max_length=50, )
    password2 = forms.CharField(max_length=50, )

    def __init__(self, user, *args, **kwargs):
        self.user = user
        super().__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super().clean()
        old_password = cleaned_data.get('old_password')
        password1 = cleaned_data.get('password1')
        password2 = cleaned_data.get('password2')
        # Validate new user password

        if password1 and password2 and old_password:
            if not self.user.check_password(old_password):
                raise ValidationError("Invalid old password, please enter your current password")
            elif password1 != password2:
                raise ValidationError("Password mismatch, please re-type same password")
        password_validation.validate_password(password1, self.user)
        return cleaned_data

    def save(self, commit=True):
        new_password = self.cleaned_data.get('password2')
        self.user.set_password(new_password)
        if commit:
            self.user.save()
        return self.user

项目/应用程序/views.py

class ChangePasswordView(LoginRequiredMixin, View):
    def get(self, request, *args, **kwargs):
        form = ChangePasswordForm(request.user, request.POST)
        context = {
            'form': form,
        }
        return render(request, 'auth/change_password.html', context)

    def post(self, request, *args, **kwargs):
        form = ChangePasswordForm(request.user, request.POST)
        if form.is_valid():
            user = form.save()
            # log user out of all active sessions
            update_session_auth_hash(request, user)
            messages.success(request, "Password updated successfully")
            return redirect('custom_redirect')
        return redirect('custom_redirect')

HTML模板渲染

<form class="card-style mb-30" action="{% url 'account_change_password' %}" method="post">
   {% csrf_token %}
   <h6 class="mb-25">Change Password</h6>

   {% if form.errors %}
   <div class="text-sm mb-25 alert alert-danger "> <i class="lni lni-close"></i>
     <span>{{form.errors}}</span>
   </div>

   {% for field in form %}
   {% for error in field.errors %}

   <div class="text-sm mb-25 alert alert-danger "> <i class="lni lni-close"></i>
     <span>{{error|escape}}</span>
   </div>
   {% endfor %}
   {% endfor %}
   {% endif %}

   {% if form.non_field_errors %}
   <div class="text-sm mb-25 alert alert-danger "> <i class="lni lni-close"></i>
     <span>{{form.non_field_errors}}ghgh </span>
   </div>
   {% endif %}

   <div class="input-style-3">
     <input type="password" placeholder="Enter old password" name="old_password" required>
     <span class="icon"><i class="lni lni-key"></i></span>
   </div>
   <div class="input-style-3">
     <input type="password" placeholder="Enter new password" name="password1" required>
     <span class="icon"><i class="lni lni-key"></i></span>
   </div>
   <div class="input-style-3">
     <input type="password" placeholder="Enter new password again" name="password2" required>
     <span class="icon"><i class="lni lni-key"></i></span>
   </div>
   <div class="col-12">
     <div class="button-group d-flex justify-content-center flex-wrap">
       <button type="submit" type="submit" class="main-btn primary-btn btn-hover m-2">
         Change Password
       </button>
     </div>
   </div>
 </form>

我面临的主要问题是表单验证在GET方法上被调用,在尝试随机尝试修复这个问题后,我意识到这个问题是由于我调用Superclass __init __方法的方式造成的。每次我删除super().__init__(*args, **kwargs)行时,我都会得到一条错误消息,说**'ChangePasswordForm'对象没有属性'_errors'**
请注意,我一直在使用上面示例中的模式将表单渲染到django模板,并且没有遇到任何问题。

utugiqy6

utugiqy61#

get()方法中没有表单数据,所以从get()方法中删除request.POST,并在**post()**方法中处理错误。

class ChangePasswordView(LoginRequiredMixin, View):

    def get(self, request, *args, **kwargs):
        form = ChangePasswordForm(request.user)
        context = {
            'form': form,
        }
        return render(request, 'auth/change_password.html', context)

    def post(self, request, *args, **kwargs):
        form = ChangePasswordForm(request.user, request.POST)
        if not form.is_valid():
            context = {
                'form': form,
            }
            return render(request, 'auth/change_password.html', context)
            
        user = form.save()
        # log user out of all active sessions
        update_session_auth_hash(request, user)
        messages.success(request, "Password updated successfully")
        return redirect(reverse('custom_redirect'))
rxztt3cl

rxztt3cl2#

return redirect('custom_redirect'))替换为return redirect('custom_redirect')

相关问题