前言
之前学习了如何管理数据库,但实际中更多的是对数据库的操作,现在开始学习操作数据库。
一、环境
flask 2.0.2 python 3.7
二、步骤
接着上一篇博客flask蓝图的使用的代码继续写,文件目录结构如下:
1.创建数据表
我比较懒,所以就改一下test表好了 models.py
from . import db
class Test(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(10))
password = db.Column(db.String(20))
然后运行命令将更新提交到数据库
python manage.py db migrate
python manage.py db upgrade
2.创建应用
创建一个名为test的文件夹作为应用,目录结构如下 编写视图函数和创建蓝图 1__init__.py
from flask import Blueprint
test_blue = Blueprint('test', __name__, url_prefix='/test')
from . import views
view.py
from flask import request, render_template, redirect, session, url_for
from . import test_blue
from flask_teach import db, models
@test_blue.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'GET':
return render_template('test/login.html')
else:
name = request.form['name']
password = request.form['password']
test = models.Test.query.filter_by(name=name).first()
if test:
if test.password == password:
session['name'] = name
return redirect('/test/index')
return '用户名不存在或者密码错误'
@test_blue.route('/register', methods=['POST', 'GET'])
def register():
if request.method == 'GET':
return render_template('test/register.html')
else:
name = request.form['name']
password = request.form['password']
test = models.Test(name=name, password=password)
db.session.add(test)
return "注册成功!!!"
@test_blue.route('/index')
def index():
return render_template('test/index.html', name=session.get('name'))
@test_blue.route('/login_out')
def login_out():
if session.get('name'):
session.pop('name')
return redirect(url_for('test.login'))
再创建一个templates目录存放模板 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h2>你好:{{name}}</h2>
<a href="/test/login_out">退出登录</a>
</body>
</html>
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="/test/login" method="post">
用户名:<input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/test/register" method="post">
用户名:<input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
因为上面用的了session,所以要加密钥加密session,不然会报错,在config.py里面加密钥。 先创建存放通用函数的py文件common.py,以后有什么公用方法函数都能放到里面。 common.py
"""存放通用方法"""
import random
import string
def random_num(count):
str_list = [random.choice(string.digits + string.ascii_letters) for i in range(count)]
random_str = ''.join(str_list)
return random_str
设置密钥 config.py
from flask_teach.common import random_num
class Config(object):
DEBUG = True
SECRET_KEY = random_num(16)
SQLALCHEMY_DATABASE_URI = "mysql://root:123456@127.0.0.1:3306/flask_teach"
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
最后注册路由 register_blueprints.py:
from flask_teach import helloworld, test
def reg_blueprint(app):
app.register_blueprint(helloworld.hello_blue)
app.register_blueprint(test.test_blue)
return app
运行
注册
数据库里面是多了条数据 登录
大功告成,有什么问题欢迎在评论区留言。 下一篇:flask自定义错误页
|