修改密码中旧密码不能成功验证

来源:7-14 .修改密码-

Jarrott

2017-08-10

老师,在修改密码部分,旧密码提交后,并不能验证,是怎么回事?

http://szimg.mukewang.com/598bbb3d0001e26315160802.jpg

http://szimg.mukewang.com/598bbb3d000114e112200762.jpg


写回答

3回答

rustgopy

2017-08-10

同学,你好,注意一下旧密码命名是old_pwd不是pwd,表单的代码如下:

class PwdForm(FlaskForm):
    old_pwd = PasswordField(
        label="旧密码",
        validators=[
            DataRequired("请输入旧密码!")
        ],
        description="旧密码",
        render_kw={
            "class": "form-control",
            "placeholder": "请输入旧密码!",
        }
    )
    new_pwd = PasswordField(
        label="新密码",
        validators=[
            DataRequired("请输入新密码!")
        ],
        description="新密码",
        render_kw={
            "class": "form-control",
            "placeholder": "请输入新密码!",
        }
    )
    submit = SubmitField(
        '编辑',
        render_kw={
            "class": "btn btn-primary",
        }
    )

    def validate_old_pwd(self, field):
        from flask import session
        pwd = field.data
        name = session["admin"]
        admin = Admin.query.filter_by(
            name=name
        ).first()
        if not admin.check_pwd(pwd):
            raise ValidationError("旧密码错误!")

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">
                <font style="color:red">{{ err }}</font>
            </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">
                <font style="color:red">{{ err }}</font>
            </div>
            {% endfor %}
        </div>
    </div>
    <div class="box-footer">
        {{ form.csrf_token }}
        {{ form.submit }}
    </div>
</form>


0
2
rustgopy
回复
Jarrott
加油,有问题与我反馈!
2017-08-10
共2条回复

Jarrott

提问者

2017-08-10

@admin.route('/pwd/', methods=["GET", "POST"])
@admin_login_req
def pwd():
    form = PwdForm()
    if form.validate_on_submit():
        form = form.data
    return render_template("admin/pwd.html", form=form)
{% extends "admin/home.html" %}
{% block content %}
    <!--内容-->
    <section class="content-header">
        <h1>微电影管理系统</h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 个人资料</a></li>
            <li class="active">修改密码</li>
        </ol>
    </section>
    <section class="content" id="showcontent">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">修改密码</h3>
                    </div>
                    <form role="form" method="post">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="input_pwd">{{ form.pwd.label }}</label>
                                {{ form.pwd }}
                                {% for err in form.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>
                        <div class="box-footer">
                            {{ form.csrf_token }}
                            {{ form.submit }}
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
    <!--内容-->
{% endblock %}


0
0

rustgopy

2017-08-10

同学,你好,你检测old_pwd是否提交到服务器端,你截取一下修改密码的视图代码以及html代码给我!

0
1
Jarrott
老师,两端代码我已经提交到回答里面了。这里粘贴不了。
2017-08-10
共1条回复

Python Flask 构建微电影视频网站

Python 最好用轻量级框架,让你轻松胜任 Python Web 工程师

683 学习 · 663 问题

查看课程