如何在Flask Blueprint中正确路由多个应用程序



我刚刚重新安排了我的flask站点,以支持多个应用程序,但似乎在多个应用的蓝图和路由方面存在问题。下面会运行,但当您转到localhost/backstory时,呈现的索引是主索引,而不是backstory索引。我试着做了一些事情,比如在蓝图注册中使用prefixurl,但似乎不起作用。

从我所看到的,Blueprint((函数指向右边的目录,并且应该引用路由/背景故事的右边文件夹中的索引。然而,它并没有这么做。我错过了什么?

from flask import Flask
from database import database
# blueprint import
from apps.home.views import home_blueprint
from apps.backstory.views import backstory_blueprint
application = Flask(__name__)
# setup with the configuration provided
application.config.from_object('config.DevelopmentConfig')
# setup all our dependencies
database.init_app(application)
# register blueprint
application.register_blueprint(home_blueprint)
application.register_blueprint(backstory_blueprint)

if __name__ == "__main__":
application.run()

主页

from flask import Blueprint, request, url_for, redirect, render_template, flash
home_blueprint = Blueprint('home', __name__, template_folder="templates/home")

@home_blueprint.route("/")
def home():
return render_template('index.html')

后台

from flask import Blueprint, request, url_for, redirect, render_template, flash
backstory_blueprint = Blueprint('backstory', __name__, template_folder="templates/backstory")

@backstory_blueprint.route("/backstory")
def backstory():
return render_template('index.html')

结构

Project
apps
backstory
templates
backstory
index.html
views.py
home
templates
home
index.html
views.py
application.py

我似乎有错误的templates_folder位置。本应将其指向模板,然后根据Flask上的文档将backstory/index.html作为render_template((位置。

然后,当您想要渲染模板时,使用admin/index.html作为查找模板的名称。如果在加载时遇到问题正确的模板启用EXPLAIN_TEMPLATE_LOADING配置变量,它将指示Flask打印出它所执行的步骤通过来定位每个render_template调用上的模板。

from flask import Blueprint, request, url_for, redirect, render_template, flash
backstory_blueprint = Blueprint('backstory', __name__, template_folder="templates")

@backstory_blueprint.route("/backstory")
def backstory():
return render_template('backstory/index.html')

最新更新