美文网首页Flask 教程
20、Flask构建弹幕微电影网站-会员修改密码,表单下拉列表同

20、Flask构建弹幕微电影网站-会员修改密码,表单下拉列表同

作者: 攻城狮笔记 | 来源:发表于2019-04-13 15:45 被阅读5次

    百度云搜索,搜各种资料:http://www.81ad.cn

    Flask 构建微电影视频网站

    已上线演示地址: http://movie.tbquan.cn

    修改密码

    修改密码

    创建修改密码的表单

    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密码更新表单

    <form role="form" method="post">
        <div class="box-body">
            <div class="form-group">
                <label for="input_pwd">{{ form.old_pwd.label }}</label>
                {{ form.old_pwd }}
                {% for err in form.old_pwd.errors %}
                    <div class="col-md-12" style="color: red">{{ err }}</div>
                {% endfor %}
            </div>
            <div class="form-group">
                <label for="input_newpwd">{{ form.new_pwd.label }}</label>
                {{ form.new_pwd }}
                {% for err in form.new_pwd.errors %}
                    <div class="col-md-12" style="color: red">{{ err }}</div>
                {% endfor %}
            </div>
        </div>
        {{ form.csrf_token }}
        <div class="box-footer">
            {{ form.submit }}
        </div>
    </form>
    
    
    BLOG_20181111_202605_10

    修改login.html密码修改登录提示

    增加flash的提示

    {% include 'admin/alert_info.html' %}
    
    
    BLOG_20181111_202612_50

    后台页面显示登录用户

    修改base.html增加显示登录名,两种方式都可使用

    <span class="hidden-xs">{{ session.login_admin }}</span>
    
    <p>
        {{ session['login_admin'] }}
        <small>2018-08-08</small>
    </p>
    
    <p>用户{{ session.login_admin }}</p>
    
    
    BLOG_20181111_202630_48

    处理表单下拉列表与数据库不同步问题

    当增加标签后,如果不重启服务器,则标签选项不会变化

    BLOG_20181111_202639_34
    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()]
    
    

    相关文章

      网友评论

        本文标题:20、Flask构建弹幕微电影网站-会员修改密码,表单下拉列表同

        本文链接:https://www.haomeiwen.com/subject/pyxrwqtx.html