Django 用户修改密码

html:

{#  修改密码  #}
    <div id="changePasswordDiv" style="margin-left: 20px; margin-top: 20px; display: none;">
        <div class="input-group" style="margin-bottom: 5px; width: 253px;">
            <span class="input-group-addon" style="width: 80px;">旧密码</span>
            <input type="password" name="oldPassword" id="oldPassword" class="form-control" />
        </div>
        <div class="input-group" style="margin-bottom: 5px; width: 253px;">
            <span class="input-group-addon" style="width: 80px;">新密码</span>
            <input type="password" name="newPassword" id="newPassword" class="form-control" />
        </div>
        <div class="input-group" style="margin-bottom: 5px; width: 253px;">
            <span class="input-group-addon" style="width: 80px;">确认密码</span>
            <input type="password" name="newPasswordAgain" id="newPasswordAgain" class="form-control" />
        </div>
        <div id="changePasswordAlert" class="alert alert-danger" role="alert" style="width: 30%; margin-bottom: 6px; display: none;"></div>
        <button type="button" id="changePasswordBtn" class="btn btn-default" data-toggle="modal" data-target="#alertTip" data-whatever="重置密码?" style="width: 100px; margin-left: 70px;">提&nbsp;&nbsp;交</button>
    </div>

前端js代码:

// 修改密码
function changePassword() {
    var changePasswordAlert = '';
    $('#changePasswordAlert').hide();
    if ( !$('#oldPassword').val() ) {
        changePasswordAlert += '**  旧密码不能为空!<br />';
    }
    if ( !$('#newPassword').val() ) {
        changePasswordAlert += '**  新密码不能为空!<br />';
    }
    if ( !$('#newPasswordAgain').val() ) {
        changePasswordAlert += '**  确认密码不能为空!<br />';
    }
    if ( $('#newPassword').val() != $('#newPasswordAgain').val() ) {
        changePasswordAlert += '**  两次密码不一致!<br />';
    }
    if ( $('#oldPassword').val() == $('#newPasswordAgain').val() ) {
        changePasswordAlert += '**  新密码和旧密码不能一样!<br />';
    }
    if (changePasswordAlert) {
        $('#changePasswordAlert').html(changePasswordAlert);
        $('#changePasswordAlert').show();

    } else {
        $.ajax({
            url: '/changePassword',
            type: 'POST',
            data: {
                username: $('#loginUsername').text().split(' ')[0],
                oldPassword: $('#oldPassword').val(),
                newPassword: $('#newPassword').val()
            },
            success: function (data, textStatus) {
                if (data == 1) {
                    alert('修改成功!');
                    window.location.href = 'index';

                } else if (data == -1) {
                    alert('旧密码错误!');

                } else if (data == -2) {
                    alert('没有相关权限!');
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        })
    }
}

后端python–view视图:

# 修改密码
@login_required(login_url='slg:login')
@require_http_methods(["POST"])
@permission_required('slg.views_slg_manager_tem', login_url='slg:get_permissionDenied')
def change_password(request):
    username = request.POST['username']
    oldPassword = request.POST['oldPassword']
    newPassword = request.POST['newPassword']
    changeResult = db_change_password(username, oldPassword, newPassword)
    return HttpResponse(changeResult)

后端python–models视图:

# 修改密码
def db_change_password(username, oldPassword, newPassword):
    user = authenticate(username=username, password=oldPassword)
    if user is not None:
        if user.is_active:
            user.set_password(newPassword)
            user.save()
            return 1    # 修改成功,允许特殊符号
        else:
            return -2   # 没有权限
    else:
        return -1      # 旧密码错误

备注:
1. 模态框 等html和js代码,参考:Django 创建/删除用户

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值