1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| 1.修改头像 urls: re_path(r'^set/avatar/', views.set_avatar),
后端 @login_required def set_avatar(request): if request.method == 'POST': file_obj = request.FILES.get('avatar') user_obj = request.user user_obj.avatar = file_obj user_obj.save() return redirect('/home/') blog = request.user.blog username = request.user.username return render(request, 'set_avatar.html', locals()) re_path(r'^set_pwd/', views.set_pwd, name='set_pwd_view'),
2.修改密码 urls:re_path(r'^set_pwd/', views.set_pwd, name='set_pwd_view'), 后端 @login_required() def set_pwd(request): if request.method == 'POST': back_dic = {'code': 10000, 'msg': ''} 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'] = '修改成功' back_dic['url'] = '/login' else: back_dic['code'] = 10001 back_dic['msg'] = '两次密码不一致' else: back_dic['code'] = 10002 back_dic['msg'] = '原密码不正确' return JsonResponse(back_dic)
|