register_blueprint不添加路由到Flask应用程序 [英] register_blueprint doesn't add route to Flask app

查看:167
本文介绍了register_blueprint不添加路由到Flask应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一条路线创建了一个蓝图,并用我的Flask应用程序注册了蓝图。但是,当请求路由时,我得到了一个404。我的代码有什么问题?

  from flask import烧瓶,蓝图

bp =蓝图(' test',__name__)
app = Flask(__ name__)
app.register_blueprint(test_blueprint,url_prefix ='/ test')

@ bp.route('/')
def home_route():
return这是本地路由。
$ b $ if if __name__ =='__main__':
app.run()



  127.0.0.1  -   -  [21 / Jun / 2016 13:54:19]GET / test HTTP / 1.1404  -  


解决方案

您在注册执行路由的代码之前注册蓝图。在蓝图完全定义之后移动 register_blueprint
$ b

  bp =蓝图('test',__name__,url_prefix ='/ test')

@ bp.route('/')
def home():
return'Hello,World! '

app.register_blueprint(bp)


I created a blueprint with a route and registered the blueprint with my Flask app. However, I get a 404 when requesting the route. What is wrong with my code?

from flask import Flask, Blueprint

bp = Blueprint('test', __name__)
app = Flask(__name__)
app.register_blueprint(test_blueprint, url_prefix='/test')

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

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

127.0.0.1 - - [21/Jun/2016 13:54:19] "GET /test HTTP/1.1" 404 -

解决方案

You registered the blueprint before the code that registered the route executed. Move register_blueprint after the blueprint has been fully defined.

bp = Blueprint('test', __name__, url_prefix='/test')

@bp.route('/')
def home():
    return 'Hello, World!'

app.register_blueprint(bp)

这篇关于register_blueprint不添加路由到Flask应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆