参考其他人分享的函数视图的版本,我改写了类视图版本。

Django 提供了修改密码的表单封装,所以自己不用重复造轮子,直接调用就可以。

视图

from django.shortcuts import render, redirect
from django.views import View
from django.contrib import messages
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm

class ChangePassword(View):

    def get(self, request):
        context = dict()
        context['form'] = PasswordChangeForm(request.user)
        return render(request, 'users/change-password.html', context=context)

    def post(self, request):
        form = PasswordChangeForm(request.user, request.POST)
        if form.is_valid():
            user = form.save()
            update_session_auth_hash(request, user)
            messages.success(request, '密码修改成功!')
        else:
            messages.error(request, '请检查表单中的错误!')
        return redirect('changepassword')

路由

from django.urls import path

from . import views

urlpatterns = [
    path('change-password', views.ChangePassword.as_view(), name='changepassword'),
]

模板

<div class="uk-card uk-card-default uk-card-body">
    <h3 class="uk-heading-divider"><span uk-icon="icon: lock;"></span> 修改密码</h3>
    {% if messages %}
    {% for m in messages %}
    <div class="uk-alert-{%if m.tags == 'error' %}danger{% else %}success{% endif %}" uk-alert>
        <a class="uk-alert-close" uk-close></a>
        <p>{{ m }}</p>
    </div>
    {% endfor %}
    {% endif %}
    
    <form action="" method="post">
        {% csrf_token %}
        {% for item in form %}
        <div class="uk-margin">
            <label class="uk-form-label" for="{{ item.name }}">{{ item.label_tag }}</label>
            <div class="uk-form-controls uk-margin-small">
                <input class="uk-input uk-width-1-3@s" id="{{ item.name }}" name="{{ item.name }}"
                    type="{{ item.field.widget.input_type }}" required>
            </div>
            <div class="uk-text-small uk-text-danger">
                {{ item.errors }}
            </div>
        </div>
        {% endfor %}
        <div class="uk-margin">
            <button class="uk-button uk-button-primary" type="submit">提交</button>
        </div>
    </form>
</div>

Reference