Flask开发微电影网站(四) (2)

可以通过调用PwdForm表单类直接在前端页面上渲染生成用户修改密码需要的字段标签

from flask_wtf import FlaskForm from wtforms.fields import StringField, PasswordField, SubmitField, FileField, TextAreaField from wtforms.validators import DataRequired, EqualTo, Email, Regexp, ValidationError 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-success" } ) 3.2 定义用户修改密码的视图函数pwd @home.route("/pwd/", methods=['GET', 'POST']) def pwd(): form = PwdForm() if form.validate_on_submit(): data = form.data user = User.query.filter_by(name=session.get("user")).first() if not user.check_pwd(data.get("pwd")): flash("旧密码输入错误,请重新输入!", "err") return redirect(url_for("home.pwd")) from werkzeug.security import generate_password_hash user.pwd = generate_password_hash(data.get("new_pwd")) db.session.add(user) db.session.commit() flash("修改密码成功,请重新登录!", "ok") return redirect(url_for("home.logout")) return render_template("home/pwd.html", form=form) 3.3 定义用户修改密码的前端页面的代码 {% extends "home/home.html" %} {% block content %} {% block css %} <style> .col-lg-1, .col-lg-10, .col-lg-11, .col-lg-12, .col-lg-2, .col-lg-3, .col-lg-4, .col-lg-5, .col-lg-6, .col-lg-7, .col-lg-8, .col-lg-9, .col-md-1, .col-md-10, .col-md-11, .col-md-12, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-sm-1, .col-sm-10, .col-sm-11, .col-sm-12, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-xs-1, .col-xs-10, .col-xs-11, .col-xs-12, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9{ padding-right: 3px; padding-left: 3px; } </style> {% endblock%} {% include "home/menu.html" %} <div> <div> <div> <h3><span></span>&nbsp;会员中心</h3> </div> <div> <form role="form"> <fieldset> <div> <label for="input_oldpwd"><span></span>&nbsp;旧密码</label> <input placeholder="旧密码" type="password" autofocus> </div> <div></div> <div> <label for="input_newpwd"><span></span>&nbsp;新密码</label> <input placeholder="新密码" type="password" autofocus> </div> <div></div> <a href="http://www.likecs.com/login.html"><span></span>&nbsp;修改密码</a> </fieldset> </form> </div> </div> </div> {% endblock %} {% block js %} <script> $(document).ready(function(){ $('#m-2').addClass('active') }); </script> {% endblock %} 4. 定义用户查看自己评论的页面代码 4.1 定义用户查看自己评论的视图函数

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpzygy.html