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 創建/刪除用戶

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章