###视图界面1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from flask import Blueprint, redirect, request, render_template, session, url_for
# 模块化管理路由
# 第一步:生成蓝图对象
from app.models import db
blueprint = Blueprint('app', __name__)
@blueprint.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
if request.method == 'POST':
# 模拟登陆
username = request.form.get('username')
password = request.form.get('password')
if username == 'coco' and password == '123456':
session['user_id'] = 1
return redirect(url_for('app.index'))
@blueprint.route('/index/')
def index():
return render_template('index.html')
@blueprint.route('/temp/')
def temp():
content = [65, 125, 145, 2522]
content_h2 = '<h2>天天都好</h2>'
content_h2_new = ' <h2>天天都好</h2>'
num = 10
return render_template('temp.html', c=content,
content_h2=content_h2,
content_h2_new=content_h2_new,
num=num)
@blueprint.route('/create_db/')
def create_db():
db.create_all()
return '创建数据库成功'
@blueprint.route('/drop_db/')
def drop_db():
db.drop_all()
return '删除数据库成功'
models(数据库)
1 |
|
###manage.py文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import redis
from flask import Flask
from flask_script import Manager
from app.models import db
from app.views import blueprint
from flask_session import Session
app = Flask(__name__)
# 蓝图使用第二步:注册蓝图
app.register_blueprint(blueprint=blueprint, url_prefix='/app/')
app.secret_key = 'qwertyuiopsdfghjklcvbnmhikkkolkyuhjiko'
# 配置session
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_REDIS'] = redis.Redis(host='127.0.0.1', port=6379)
# 配置数据库
# dialect+driver://username:password@host:port/database
app.config['SQLALCHEMY_DATABASE_URI']='mysql+pymysql://root:123456@127.0.0.1:3306/flask7'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# 初始化session和app对象
se = Session()
se.init_app(app)
# 初始化db和app对象
db.init_app(app)
manage = Manager(app)
if __name__ == '__main__':
manage.run()