我正在Flask中学习蓝图,并编写了以下简短的脚本来测试如何从蓝图中提供网址:
from flask import Flask, Blueprint
app = Flask(__name__)
bp = Blueprint('bp', __name__)
app.register_blueprint(bp, url_prefix='/bp')
@bp.route('/', methods=['GET', 'POST'])
def bp_home():
return("And a new blueprint is born!")
@app.route('/', methods=['GET', 'POST'])
def app_home():
return("App home is here!")
app.run()
我期待虽然http://localhost:5000
应该给我字符串“App home is here!”,http://localhost:5000/bp
应该返回“并且新的蓝图诞生了!”。不幸的是,只有前者有效;后者给了我一个404.我做错了什么?
答案 0 :(得分:1)
您必须在之后调用app.register_blueprint
您已定义蓝图的路线。
from flask import Flask, Blueprint
app = Flask(__name__)
bp = Blueprint('bp', __name__)
@bp.route('/', methods=['GET', 'POST'])
def bp_home():
return("And a new blueprint is born!")
@app.route('/', methods=['GET', 'POST'])
def app_home():
return("App home is here!")
app.register_blueprint(bp, url_prefix='/bp')
app.run()