个人博客,欢迎查看:https://blog.starmeow.cn/
Github地址:https://github.com/xyliurui/FlaskMovie
修改密码
修改密码
创建修改密码的表单
class PwdForm(FlaskForm):
old_pwd = PasswordField(
label='旧密码',
validators=[
DataRequired('请输入旧密码!')
],
description='旧密码',
render_kw={
'class': "form-control",
'placeholder': "请输入旧密码",
'required': "required"
}
)
new_pwd = PasswordField(
label='新密码',
validators=[
DataRequired('请输入新密码!')
],
description='新密码',
render_kw={
'class': "form-control",
'placeholder': "请输入新密码",
'required': "required"
}
)
submit = SubmitField(
label='提交',
render_kw={
'class': "btn btn-primary"
}
)
def validate_old_pwd(self, field):
"""检查验证旧密码是否正确"""
from flask import session
old_pwd = field.data
login_name = session['login_admin']
admin = Admin.query.filter_by(name=login_name).first()
if not admin.check_pwd(old_pwd):
raise ValidationError('旧密码错误!')
修改pwd密码更新视图
@admin.route("/pwd/", methods=['GET', 'POST'])
@admin_login_require
def pwd():
form = PwdForm()
if form.validate_on_submit():
data = form.data
login_name = session['login_admin']
admin = Admin.query.filter_by(name=login_name).first()
from werkzeug.security import generate_password_hash
admin.pwd = generate_password_hash(data['new_pwd'])
db.session.commit() # 提交新密码保存,然后跳转到登录界面
flash('密码修改成功,请重新登录!', category='ok')
return redirect(url_for('admin.logout'))
return render_template('admin/pwd.html', form=form)
修改pwd.html密码更新表单
修改login.html密码修改登录提示
增加flash
的提示
{% include 'admin/alert_info.html' %}
后台页面显示登录用户
修改base.html增加显示登录名,两种方式都可使用
{{ session.login_admin }}
{{ session['login_admin'] }}
2018-08-08
用户{{ session.login_admin }}
处理表单下拉列表与数据库不同步问题
当增加标签后,如果不重启服务器,则标签选项不会变化
class MovieForm(FlaskForm):
tag_id = SelectField(
label='标签',
validators=[
DataRequired('请选择标签!')
],
coerce=int,
# choices=[(tag.id, tag.name) for tag in Tag.query.all()],
description='标签',
render_kw={
'class': "form-control"
}
)
def __init__(self, *args, **kwargs):
super(MovieForm, self).__init__(*args, **kwargs)
self.tag_id.choices = [(v.id, v.name) for v in Tag.query.all()]