如何在Django中显示change_password中的错误消息

时间:2021-10-24 03:40:18

I'm struggling to show error message in change_password in Django.

我很难在Django的change_password中显示错误消息。

I tried all the ways I know to show errors in the template file, but nothing shows up when I put some wrong information on purpose. I thought it's because of redirecting when the form is not valid. But, the changing password feature doesn't work without the redirecting.

我尝试了所有我知道的在模板文件中显示错误的方法,但是当我故意放置一些错误的信息时什么也没有显示。我认为这是因为当表单无效时重新定向。但是,如果没有重定向,修改密码功能就不能工作。

Can anyone suggest a way to do that?

谁能提出一个办法吗?

views.py

views.py

def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return redirect('/accounts/profile')
        else:
            return redirect('/accounts/change-password')

    else:
        form = PasswordChangeForm(user=request.user)

        args = {'form': form}
        return render(request, 'accounts/change_password.html', args)

HTML template

HTML模板

<form method="POST">
    {% csrf_token %}
    <p class="error-message">
        {{ form.errors.old_password }}
        {{ form.errors.new_password1 }}
        {{ form.errors.new_password2 }}
        {{ form.non_field_errors }}
        {% if form.non_field_errors %}
            {% for error in form.non_field_errors %}
                {{ error }}
            {% endfor %}
        {% endif %}
    </p>
    <div class="form-group row">
        <label for="inputPassword" class="col-sm-3 col-form-label">Old Password</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="old_password" placeholder="Old Password" required autofocus>
        </div>
    </div>
    <div class="form-group row">
        <label for="inputPassword" class="col-sm-3 col-form-label">New Password</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="new_password1" placeholder="New Password" required>
        </div>
    </div>
    <div class="form-group row">
        <label for="inputPassword" class="col-sm-3 col-form-label">Confirm New Password</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="new_password2" id="inputPassword" placeholder="Confirm New Password" required>
        </div>
    </div>

    <input class="btn btn-modvisor btn-block" type="submit" name="" value="Submit">
</form>

1 个解决方案

#1


1  

Try without redirecting when there is an error, like this:

当出现错误时,不要重定向,比如:

def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return redirect('/accounts/profile')
    else:
        form = PasswordChangeForm(user=request.user)

    args = {'form': form}
    return render(request, 'accounts/change_password.html', args)

As you can see, like this the line return render(... is also called when the form is not valid.

如您所见,像这样行返回渲染(……)当窗体无效时也调用。

#1


1  

Try without redirecting when there is an error, like this:

当出现错误时,不要重定向,比如:

def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return redirect('/accounts/profile')
    else:
        form = PasswordChangeForm(user=request.user)

    args = {'form': form}
    return render(request, 'accounts/change_password.html', args)

As you can see, like this the line return render(... is also called when the form is not valid.

如您所见,像这样行返回渲染(……)当窗体无效时也调用。