- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想实现简单的网站布局:
/
必须呈现 home.html
/one
, /two
, /three
必须呈现 one.html
, two.html
, three.html
相应地
到目前为止,我想出了以下代码:
main_page = Blueprint('main', __name__)
category_page = Blueprint('category', __name__)
@main_page.route("/")
def home():
return render_template('home.html')
@category_page.route('/<category>')
def show(category):
return render_template('{}.html'.format(category))
app = Flask(__name__)
app.register_blueprint(main_page, url_prefix='/')
app.register_blueprint(category_page, url_prefix='/categories')
这样我就可以路由 categories
至 /categories/<category>
.我怎样才能将它们路由到 /<category>
相反,同时保持 home.html
链接到 /
?感谢您的帮助
我尝试了两种方法:
设置 url_prefix='/'
对于两个蓝图 => 第二个蓝图不起作用。
代替main_page
蓝图仅使用 app.route('/')
呈现 home.html
.当与 category_page
混合时,这个也不起作用蓝图
最佳答案
您可以将变量移动到注册语句中的 url_prefix
参数:
@main_page.route("/")
def home():
return render_template('home.html')
app.register_blueprint(main_page, url_prefix='/')
@category_page.route('/')
def show(category):
return render_template('{}.html'.format(category))
app.register_blueprint(category_page, url_prefix='/<category>')
(这取决于整个模式的复杂程度,但最好让带有变量的注册语句靠近每个函数以处理多个 View 。)
关于python - 两个 flask 蓝图的 url_prefix 相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46870909/
抱歉,我已经在 stackoverflow 中搜索并用谷歌搜索,但没有找到有用的信息。 我有一个 flask 应用程序, python 版本2.6 flask 版本 0.9 它的应用层次结构就像
我想使用 Flask 蓝图将 Flask-Restful 资源组织到单独的 URL 前缀中。无论我设置什么 url 前缀(在蓝图创建或注册期间),所有内容都会映射到原始路由路径。如何正确使用 Rest
我有一个只有一个 View 的蓝图。我想在 View 中获取蓝图的 url_prefix。不幸的是 test.url_prefix 返回 None。还有其他办法吗? app.register_blue
我有一个大型 flask 应用程序,其结构如下所示: app/ - mod1/ - mod2/ - init.py 每个 modX 都使用一个蓝图,该蓝图在 init.py 中注册到 Fl
我想实现简单的网站布局: /必须呈现 home.html /one , /two , /three必须呈现 one.html , two.html , three.html相应地 到目前为止,我想出了
我是一名优秀的程序员,十分优秀!