使用蓝图时找不到Flask URL

时间:2017-05-09 12:10:32

标签: python flask blueprint

这真的很奇怪,但我遇到了这个问题。一切都在工作,我几乎完成了应用程序,但突然间它停止了。我已经隔离了代码,我意识到当我注册蓝图并在路由上使用它时,它无法返回说没有找到URL。这是孤立的代码:

from flask import Flask, render_template, Blueprint

app = Flask(__name__)

home = Blueprint('home', __name__, static_folder='static', template_folder='template')
app.register_blueprint(home)


@home.route('/')  #<---This one does not work
# @app.route('/') <--- This one works
def index():
    return "This is the index route."
    # return render_template('layer.html')


if __name__ == '__main__':
    app.run()

1 个答案:

答案 0 :(得分:4)

在定义路线后移动app.register_blueprint(home)

from flask import Flask, Blueprint

app = Flask(__name__)

home = Blueprint('home', __name__, static_folder='static', template_folder='template')

@home.route('/')  
def index():
    return "This is the index route."

app.register_blueprint(home)

if __name__ == '__main__':
    app.run()