显示用户输入错误密码的错误消息

发布于 2024-10-31 04:54:52 字数 1608 浏览 0 评论 0原文

我正在尝试为用户编写一个页面来更新他们的信息并更改他们的密码。

我现在可以使用了。我提供的代码有效,但我不知道如何告诉用户他们输入了错误的密码。如果用户输入密码,则表单有效,但如果他们输入错误的密码,我想说该表单无效。但是,如果我将 forms.ValidationError("some error msg") 放在现在显示 forms.errors 的 else stmt 中,它不会返回到模板,而是在编译器消息页面中显示错误消息。

这是我的 from django.contrib.auth.models import User

def edit_page(request):

u = request.user

if request.method == 'POST':

    form = EditUserForm(request.POST)

    if form.is_valid():

        if u.check_password(form.cleaned_data['oldPassword']):

            u.set_password(form.cleaned_data['password1'])

            u.save()

            return HttpResponseRedirect('/')    

        else:

            form.errors //Where I put forms.ValidationError()   

else:

    form = EditUserForm()

variables = RequestContext(request, {

    'form': form, 'user': request.user

})      

return render_to_response(

    'registration/Edit_User.html',

    variables

)

这是在我的 forms.pyclass 中

EditUserForm(forms.Form):

oldPassword = forms.CharField(
    label=u'Current Password',
    widget = forms.PasswordInput()
)   
password1 = forms.CharField(
    label=u'New Password',
    widget=forms.PasswordInput()
)
password2 = forms.CharField(
    label=u'New Password (Again)',
    widget=forms.PasswordInput()
)

def clean_password2(self):
    if 'password1' in self.cleaned_data:
        password1 = self.cleaned_data['password1']
        password2 = self.cleaned_data['password2']
        if password1 == password2:
            return password2
    raise forms.ValidationError('Passwords do not match.')

I am trying to write a page for a user to update their information and change their password.

I have it working now. The code that I have provided works, but I do not know how to tell the user they entered the wrong password. If the user enters a password the form is valid but if they enter the wrong password I want to say the form is not valid. But if I put forms.ValidationError("some error msg") in the else stmt that right now says forms.errors, it doesn't go back to the template but displays the error msg in a compiler msg page.

This is my
from django.contrib.auth.models import User

def edit_page(request):

u = request.user

if request.method == 'POST':

    form = EditUserForm(request.POST)

    if form.is_valid():

        if u.check_password(form.cleaned_data['oldPassword']):

            u.set_password(form.cleaned_data['password1'])

            u.save()

            return HttpResponseRedirect('/')    

        else:

            form.errors //Where I put forms.ValidationError()   

else:

    form = EditUserForm()

variables = RequestContext(request, {

    'form': form, 'user': request.user

})      

return render_to_response(

    'registration/Edit_User.html',

    variables

)

This is in my forms.pyclass

EditUserForm(forms.Form):

oldPassword = forms.CharField(
    label=u'Current Password',
    widget = forms.PasswordInput()
)   
password1 = forms.CharField(
    label=u'New Password',
    widget=forms.PasswordInput()
)
password2 = forms.CharField(
    label=u'New Password (Again)',
    widget=forms.PasswordInput()
)

def clean_password2(self):
    if 'password1' in self.cleaned_data:
        password1 = self.cleaned_data['password1']
        password2 = self.cleaned_data['password2']
        if password1 == password2:
            return password2
    raise forms.ValidationError('Passwords do not match.')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拿命拼未来 2024-11-07 04:54:52

问题出在您的 clean_oldPassword 方法中。您在字符串“oldPassword”上调用check_password,而不是变量,所以自然会失败。

您还应该确保您的模板显示 form.errors,以便您了解其未验证的原因。

The problem is in your clean_oldPassword method. You call check_password on the string 'oldPassword', instead of the variable, so naturally it fails.

You should also ensure that your template displays form.errors, so you can see why it doesn't validate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文