模拟用户注册、登录:
前提已经安装好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_template
from flask_sqlalchemy import SQLAlchemy
import 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:
欢迎登录
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
{{ message }}
{% endfor %}
{% endif %}
{% endwith %}
Name
密码
密码2
邮箱
{% for user in users %}
{{ user.regName }}
{{ user.pwd }}
{{ user.pwdRepeat }}
{{ user.email }}
{% endfor %}
注册界面zhuce.html:
个人用户注册
个人用户注册
欢迎注册
已有账号? 请登录>
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
{{ message }}
{% endfor %}
{% endif %}
{% endwith %}
登录成功页面login_success.html:
登录成功
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
{{ message }}
{% endfor %}
{% endif %}
{% endwith %}
Hello,登录成功界面
退出
运行效果图截图
登录界面:
注册界面:
登录成功界面: