flask 案例
代码
auth.py
from flask import Blueprint, render_template
# /auth
bp = Blueprint("auth", __name__, url_prefix="/auth")
# 也就是前面已经定义好前缀,/auth/login
@bp.route("login")
def login():
pass
@bp.route("register")
def register():
return render_template("register.html")
# @bp.route("login")
# def register():
# return render_template("login.html")
qa.py
from flask import Blueprint
bp = Blueprint("qa", __name__, url_prefix="/")
@bp.route("/")
def index():
pass
app.py
from flask import Flask
import config
from exts import db
from models import UserModel
# 导入蓝图
from blueprints.qa import bp as qa_bp
from blueprints.auth import bp as auth_bp
from flask_migrate import Migrate
app = Flask(__name__)
app.config.from_object(config)
# 创建app后,再与db 和app绑定
db.init_app(app)
migrate = Migrate(app, db)
# blueprint,用来做模块化的
# 蓝图,视图函数模块化,蓝图
# 注册蓝图,简化app.py中的文件,便于后期维护
app.register_blueprint(qa_bp)
app.register_blueprint(auth_bp)
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
if __name__ == '__main__':
app.run()
文件结构
实例
参考文章
https://www.bilibili.com/video/BV17r4y1y7jJ