模拟用户注册、登录:前提已经安装好mysql数据库,数据库信息在config.py中设置,对应项目中需要安装数据库插件。第一次执行时,在web-test.py中打开db.create_all()这一行,会自动在数据库中创建对应的表,然后在注释掉db.create_all()。web-test.py代码如下:from flask import Flask, request, flash, url_for, redirect, render_templatefrom flask_sqlalchemy import SQLAlchemyimport config app = Flask(__name__)#数据库配置app.config.from_object(config)app.config["SECRET_KEY"] = "12345678"db = SQLAlchemy(app) class LoginUsers(db.Model): id = db.Column('student_id', db.Integer, primary_key=True) regName = db.Column(db.String(100)) pwd = db.Column(db.String(200)) pwdRepeat = db.Column(db.String(200)) email = db.Column(db.String(200)) def __init__(self, regName, pwd, pwdRepeat, email): self.regName = regName self.pwd = pwd self.pwdRepeat = pwdRepeat self.email = email @app.route('/',methods=['GET','POST'])def login_test(): if request.method == 'POST': regName = request.form['regName'] pwd = request.form['pwd'] loginUser = LoginUsers.query.filter_by(regName=regName).first() if not regName or not pwd: flash('输入信息不全,请重新输入', 'warning') elif not loginUser: flash('用户不存在', 'warning') else: if loginUser.pwd != pwd: flash('密码错误,请重新输入', 'warning') else: flash('登录成功!!') return redirect(url_for('login_success')) return render_template('login_test.html', users=LoginUsers.query.all()) @app.route('/zhuce/', methods=['GET', 'POST'])def zhuce(): if request.method == 'POST': regName = request.form['regName'] pwd = request.form['pwd'] pwdRepeat = request.form['pwdRepeat'] email = request.form['email'] loginUser = LoginUsers.query.filter_by(regName=regName).first() if not regName or not pwd or not pwdRepeat or not email: flash('信息输入不全,请重新输入', 'warning') elif loginUser: flash('用户已经存在', 'warning') else: user = LoginUsers(regName, pwd, pwdRepeat, email) db.session.add(user) db.session.commit() flash('用户添加成功') return redirect(url_for('login_test')) return render_template('zhuce.html') @app.route('/login_success/',methods=['GET','POST'])def login_success(): return render_template('login_success.html') #db.create_all() if __name__ == '__main__': app.run(debug=None)数据库配置信息config.py(config.py与web-test.py都在项目的根目录下):DIALECT = 'mysql' # 要用的什么数据库DRIVER = 'pymysql' # 连接数据库驱动USERNAME = 'root' # 用户名PASSWORD ='1111111' # 密码HOST = '127.0.0.1' # 服务器PORT ='3306' # 端口DATABASE = 'flask_books' # 数据库名 SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE)SQLALCHEMY_TRACK_MODIFICATIONS = False注:login-test.html、zhuce.html、login_success.html三个文件都在templates目录下。登录界面login-test.html:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>欢迎登录</title></head><body><!-- 以下是flask消息闪现在模板中接收消息,即flash()的消息会在此显示 --><b> {% with messages = get_flashed_messages() %} {% if messages %} {% for message in messages %} {{ message }} {% endfor %} {% endif %}{% endwith %}</b> <form method="post"> <input type = "text" name = "regName" placeholder="您的账户名和登录名" /><br> <input type = "text" name = "pwd" placeholder="密码" /><br> <input type = "submit" value = "登 录" /> </form> <div class="item item-fore5"> <a href="" class="" target="_blank">忘记密码</a> </div> <div class="item item-fore5"> <a href="http://127.0.0.1:5000/zhuce/" >注 册</a> </div><!-- 以下是将添加的用户信息显示在浏览器中 --> <table> <thead> <tr> <th>Name</th> <th>密码</th> <th>密码2</th> <th>邮箱</th> </tr> </thead> <tbody> {% for user in users %} <tr> <td>{{ user.regName }}</td> <td>{{ user.pwd }}</td> <td>{{ user.pwdRepeat }}</td> <td>{{ user.email }}</td> </tr> {% endfor %} </tbody> </table></body></html>注册界面zhuce.html:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>个人用户注册</title></head><body><h3>个人用户注册</h3> <hr/> <div class="logo-con w clearfix"> <div class="logo-title">欢迎注册</div> <div class="have-account">已有账号? <a href="http://127.0.0.1:5000/">请登录></a></div> </div><b> {% with messages = get_flashed_messages() %} {% if messages %} {% for message in messages %} {{ message }} {% endfor %} {% endif %}{% endwith %}</b> <form method = "post"> <label for = "regName">用 户 名</label> <input type = "text" name = "regName" placeholder="您的账户名和登录名" /><br> <label for = "password">设 置 密 码</label> <input type = "text" name = "pwd" placeholder = "建议使用两种或两种以上字符组合" /><br> <label for = "pwdRepeat">确 认 密 码</label> <input type = "text" name = "pwdRepeat" placeholder = "请再次输入密码" /><br> <label for = "email">邮 箱</label> <input type="text" name="email" autocomplete="off" maxlength="50" placeholder="请输入邮箱" ><br> <input type = "submit" value = "立即注册" /> </form></body></html>登录成功页面login_success.html:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>登录成功</title></head><body><b> {% with messages = get_flashed_messages() %} {% if messages %} {% for message in messages %} {{ message }} {% endfor %} {% endif %}{% endwith %}</b><br><p>Hello,登录成功界面</p><a href="http://127.0.0.1:5000/">退出</a></body></html>运行效果图截图登录界面:注册界面:登录成功界面: