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;">提 交</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 创建/删除用户