def change_password(request): if request.method == "POST": # 提交表单 form = forms.ChangePasswordForm(request.POST) if form.is_valid(): user_id = _get_current_userid(request) password = common.encode_password( form.cleaned_data['password'].strip()) old_password = User.objects.get(id=user_id).password if password != old_password: # 密码错误 form._errors["password"] = ErrorList([u"密码不正确!"]) context = RequestContext(request, {'form': form}) return render_to_response('change_password.htm', context) else: # 修改密码成功 new_password = common.encode_password( form.cleaned_data['new_password'].strip()) User.objects.filter(id=user_id).update( gmt_modify=datetime.now(), password=new_password) context = RequestContext(request, {'success': True}) return render_to_response('change_password.htm', context) else: context = RequestContext(request, {'form': form}) return render_to_response('change_password.htm', context) else: # 渲染页面 context = RequestContext(request) return render_to_response('change_password.htm', context)
def change_password(username): form=forms.ChangePasswordForm() if form.validate_on_submit(): models.User.update(password=generate_password_hash(form.password_to_change.data)).where( models.User.username == username).execute() flash('Password has been updated!', 'success') return redirect(url_for('index')) return render_template('profile.html', form=form)
def forget_password(request): if request.method == "POST": form = forms.ChangePasswordForm(request.POST) if form.is_valid(): email = request.POST.get('email') password = request.POST.get('newPassword2') u = User.objects.get(email=email) u.set_password(raw_password=password) u.save() return HttpResponse(status=200) # end if errors = form.errors.items() return HttpResponse(json.dumps(errors), status=400, content_type='application/json') # end if form = forms.ChangePasswordForm() return render(request, 'usuarios/change_password.html', {'form': form})
def profile(): form = forms.ChangePasswordForm() if form.validate_on_submit(): user = models.User.get(models.User.email == form.email.data) if user: user.password = generate_password_hash(form.password.data) user.save() flash('Password successfully changed.', 'success') return redirect(url_for('profile')) else: flash('Password change was unsuccessful.', 'error') return redirect(url_for('profile')) return render_template('profile.html', form=form)
def change_password(): form = forms.ChangePasswordForm() if request.method == 'POST' and form.validate_on_submit(): # if bcrypt.check_password_hash(current_user.password, form.current_password.data): if check_password_hash(current_user.password, form.current_password.data): # current_user.password = bcrypt.generate_password_hash(form.new_password.data) current_user.password = generate_password_hash( form.new_password.data) db.session.commit() return redirect(url_for('login')) else: form.current_password.errors.append('Incorrect password.') return render_template('main/change_password.html', form=form)
def show_profile(): form = forms.ChangePasswordForm() if request.method == 'GET': return render_template('profile.html', user=current_user, form=form) elif request.method == 'POST': if not form.validate(): flash('All fields are required.', 'warning') else: # get form data password = form.old_password.data # check that old password is correct if not current_user.authenticate(password): flash('Incorrect old password.', 'warning') # check that new passwords match elif form.new_password.data != form.new_password_v.data: flash('New passwords must match.', 'warning') else: model.change_password(current_user.user_id, form.new_password.data) flash('Your password has been changed.', 'success') # add new password to db return render_template('profile.html', user=current_user, form=form)