随着业务逻辑 的增多.视图函数不能都直接写在flask入口文件app.py中
需要按功能块将视图函数分别写到blueprint目录下单独的py文件中.
然后在app.py中对每个Blueprint对象进行注册.
# app.py
from blueprints.demo import bp as demobp
from blueprints.auth import bp as authbp
from blueprints.qa import bp as qabp
app=Flask(__name__)
app.register_blueprint(demobp) #绑定
app.register_blueprint(authbp)
app.register_blueprint(qabp)
视图函数按功能拆分到单独文件中
# blueprints/demo.py
from flask import Blueprint,render_template
bp = Blueprint('demo', __name__, url_prefix='/demo') #
@bp.route('/')
def curr_demo():
session['uname']='Yancy'
return render_template('template-demo2.html')
....
# blueprints/auth.py
from flask import Blueprint,render_template
bp = Blueprint('auth', __name__, url_prefix='/auth')
@bp.route('/login')
def login():
return render_template('login.html')
@bp.route('/resigter')
def register():
return render_template('register.html')
....
http://127.0.0.1:5000/auth/login
标签:blueprint,__,flask,demo,app,py,bp,template,功能块 From: https://www.cnblogs.com/unity-yancy/p/17232819.html