前端
<script> $('#id_edit').click(function () { $.ajax({ url:'/set_password/', type:'post', data:{ 'old_password':$('#id_old_password').val(), 'new_password':$('#id_new_password').val(), 'confirm_password':$('#id_confirm_password').val(), 'csrfmiddlewaretoken':'{{ csrf_token }}', }, success:function(args){ if (args.code==1000){ // window.location.href='/login/' window.location.reload() }else{ $('#id_password_error').text(args.msg) } } }) })
后端
@login_required def set_password(request): # if request.method=='POST': if request.is_ajax(): back_dic = {'code':1000,'msg':''} if request.method=='POST': old_password = request.POST.get('old_password') new_password = request.POST.get('new_password') confirm_password = request.POST.get('confirm_password') #校验原密码是否正确 is_right = request.user.check_password(old_password) if is_right: if new_password == confirm_password: request.user.set_password(new_password) # 如果密码相同则设置密码并保存 request.user.save() back_dic['msg'] = '修改成功' else: back_dic['code'] = 1001 back_dic['msg'] = '两次密码不一致' else: back_dic['code']= 1002 back_dic['msg'] = '原密码错误' return JsonResponse(back_dic)
标签:old,前后,request,back,dic,ajax,new,password,交互 From: https://www.cnblogs.com/shclbear/p/16785953.html