代码工作正常,只是问题在于成功操作,成功消息不起作用

时间:2019-02-06 08:57:48

标签: python django

此代码正在运行精细更新用户密码,这是成功操作时面临的唯一问题。成功消息不在页面上打印

views.py

def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(request.user, request.POST)
        if form.is_valid():
            user = form.save()
            update_session_auth_hash(request, user)  # Important!
            messages.success(request, 'Your password was successfully updated!')
            return redirect('change_password')
        else:
            messages.error(request, 'Please correct the error below.')
    else:
        form = PasswordChangeForm(request.user)
    return render(request, 'accounts/change_password.html', {
        'form': form
    })

change_password.html

{% extends 'accounts/base.html' %}

{% block content %}
<!-- {{messages}} -->

{{ pwd_updated }}
<form method="post">
  {% csrf_token %}
  {{ form }}
  <button type="submit">Save changes</button>
</form>


{% endblock %}

我尝试了这个,但是这也不起作用:

        print(messages.success(request, 'Your password was successfully updated!'))

也尝试过:

    pwd_updated = messages.success(request, 'Your password was successfully updated!')
    return redirect('change_password',{'pwd_updated':pwd_updated})

&在我的html上获取它。

但不起作用。

1 个答案:

答案 0 :(得分:3)

在change_password.html中,尝试使用以下代码:

{% block content %}
    {% for message in messages %}
        {{ message }}
    {% endfor %}

    ...

{% endblock %}

那应该有帮助。