flask项目搭建

config---配置

 1 # -*- coding = utf-8 -*-
 2 # @Time : 2020/7/28 17:51
 3 # @Author : 贾伟文
 4 # @File : config.py
 5 # @Software : PyCharm
 6 
 7 DB_URI = 'mysql://root:123456@localhost:3306/icbc?charset=utf8'
 8 SQLALCHEMY_DATABASE_URI = DB_URI
 9 SQLALCHEMY_TRACK_MODIFICATIONS = False
10 SECRET_KEY = 'abfhdj'
View Code

exts

# -*- coding = utf-8 -*-
# @Time : 2020/7/28 17:48
# @Author : 贾伟文
# @File : exts.py
# @Software : PyCharm

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
View Code

manage

# -*- coding = utf-8 -*-
# @Time : 2020/7/28 18:09
# @Author : 贾伟文
# @File : manage.py
# @Software : PyCharm

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import app
from exts import db

manager = Manager(app)
Migrate(app, db)
manager.add_command('db', MigrateCommand)


if __name__ == '__main__':
    manager.run()
View Code

app

 1 from flask import Flask, render_template, session
 2 from front import front_bp, login_required
 3 import config
 4 from exts import db
 5 
 6 app = Flask(__name__)
 7 app.config.from_object(config)
 8 app.register_blueprint(front_bp)
 9 db.init_app(app)
10 
11 
12 @app.route('/', endpoint='index')
13 @login_required
14 def hello_world():
15     user_id = session.get('user_id')
16     return render_template('front/index.html', user_id=user_id)
17 
18 
19 if __name__ == '__main__':
20     app.run(debug=True)
View Code

login_requierd---登录验证

# -*- coding = utf-8 -*-
# @Time : 2020/7/28 21:38
# @Author : 贾伟文
# @File : login_required.py
# @Software : PyCharm
from flask import session, redirect, url_for


def login_required(func):
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id")
        if not user_id:
            return redirect(url_for('front.login'))
        return func(*args, **kwargs)
    return wrapper

 

posted @ 2020-07-29 08:35  一文g  阅读(164)  评论(0编辑  收藏  举报