# 修改密码视图函数 def changepwd(request): # 处理GET请求,直接返回页面 if request.method == 'GET': form = ChangepwdForm() return render_to_response('changepwd.html', RequestContext(request, {'form': form, })) else: # 处理POST请求, # 组件校验,将浏览器传的信息通过组件校验 form = ChangepwdForm(request.POST) # 如果信息校验成功 if form.is_valid(): # 获取当前登录用户名 username = request.user.username # 获取原来密码 oldpassword = request.POST.get('oldpassword', '') # 验证用户密码正确 user = authenticate(username=username, password=oldpassword) # 如果用户密码正确 if user is not None and user.is_active: # 获取新密码 newpassword = request.POST.get('newpassword1', '') # 设置密码 user.set_password(newpassword) # 保存 user.save() # 修改成功,提醒用户 return render_to_response('changepwd.html', RequestContext(request, {'changepwd_success': True})) # 如果用户密码不正确 else: return render_to_response('changepwd.html', RequestContext(request, {'form': form, 'oldpassword_is_wrong': True})) # 如果信息校验失败 else: return render_to_response('changepwd.html', RequestContext(request, {'form': form, }))