问题描述
我还是 Flask 的新手,所以可能有一个明显的方法来实现这一点,但到目前为止我还没有从文档中弄清楚.我的应用程序分为几个大部分不同的部分,它们共享诸如用户/会话/安全性和基本模板之类的内容,但大多数情况下交互不多,应该在不同的路径下路由,例如 /part1/...
.我认为这几乎正是蓝图的用途.但是如果我需要在蓝图下进一步对路由和逻辑进行分组怎么办?
I'm still new to Flask, so there may be an obvious way to accomplish this, but I haven't been able to figure it out so far from the documentation. My app is divided into several mostly disparate parts that share things like users/sessions/security and base template and everything but mostly do not interact much, and should be routed under different paths like /part1/...
. I think this is pretty much exactly what blueprints are for. But what if I need to group routes and logic further under a blueprint?
例如,我有 blueprint1
和 url_prefix='/blueprint1'
并且可能在此之下我想要围绕用户共享照片和其他用户评论他们.我想不出比这更好的方法了:
For example, I have blueprint1
with url_prefix='/blueprint1'
and maybe under that I want to have a collection of views revolving around a user sharing photos and other users commenting on them. I can't think of a better way of doing it than:
# app/blueprints/blueprint1/__init__.py
blueprint1 = Blueprint('blueprint1', __name__, template_folder='blueprint1')
@blueprint1.route('/photos')
def photos_index():
return render_template('photos/index.html')
@blueprint.route('/photos/<int:photo_id>')
def photos_show(photo_id):
photo = get_a_photo_object(photo_id)
return render_template('photos/show.html', photo=photo)
@blueprint.route('/photos', methods=['POST'])
def photos_post():
...
这里的问题是与 blueprint1
的照片部分相关的所有视图都位于顶级",可能是视频或音频的蓝图或其他任何东西(名为 videos_index()
...).有没有办法以更分层的方式对它们进行分组,比如模板如何放在 'blueprint1/photos'
子目录下?当然,我可以将所有的照片视图放在它们自己的模块中以保持它们分开组织,但是如果我想将父 'blueprint1/photos'
路径更改为其他路径怎么办?我确定我可以发明一个函数或装饰器,将相关路由分组到同一根路径下,但是我仍然必须使用 photos_
前缀命名所有函数,并像 url_for 一样引用它们('blueprint1.photos_show')
当 Flask 应用程序变大并且您需要将相似的部分分组和划分在一起时,蓝图似乎是答案,但是当蓝图本身变大时,您无法做同样的事情.
The problem here is that all the views related to the photos section of blueprint1
are located at the "top level," right with maybe blueprints for videos or audio or whatever (named videos_index()
...). Is there any way to group them in a more hierarchical manner, like how the templates go under the 'blueprint1/photos'
sub-directory? Of course I can put all the photo views in their own module to keep them organized separately, but what if I want to change the parent 'blueprint1/photos'
path to something else? I'm sure I can invent a function or decorator that groups related routes under the same root path, but then I still have to name all the functions with the photos_
prefix and reference them like url_for('blueprint1.photos_show')
It seems like blueprints are the answer when a Flask app gets large and you need to group and compartmentalize similar parts together, but you cannot do the same thing when the blueprints themselves get large.
作为参考,在 Laravel 中,您可以在 Controller
类下将相关的视图"分组,其中视图是方法.控制器可以驻留在分层命名空间中,例如 appHttpControllersBlueprint1Photocontroller
,路由可以像
For reference, in Laravel you can group related "views" under a Controller
class where the views are methods. Controllers can reside in hierarchical namespaces like appHttpControllersBlueprint1Photocontroller
, routes can be grouped together like
Route::group(['prefix' => 'blueprint1'], function() {
Route::group(['prefix' => 'photos'], function() {
Route::get('/', ['as' => 'blueprint.photos.index', 'uses' => 'ModelApiController@index']);
Route::post('/', ['as' => 'blueprint.photos.store', 'uses' => 'ModelApiController@store']);
Route::get('/{id}', ['as' => 'blueprint.photos.get', 'uses' => 'ModelApiController@get'])
->where('id', '[0-9]+');
});
});
和路由可以像 action('Blueprint1PhotoController@index')
一样得到.
and routes can be gotten like action('Blueprint1PhotoController@index')
.
如果我能制作照片蓝图,那么只要做blueprint1.register_blueprint(photos_blueprint, url_prefix='/photos')
之类的,这些问题就基本解决了.不幸的是,Flask 似乎不支持这样的嵌套蓝图.有没有其他方法可以解决这个问题?
If only I could make a photos blueprint, then just do blueprint1.register_blueprint(photos_blueprint, url_prefix='/photos')
or the like, these problems would pretty much be solved. Unfortunately Flask does not seem to support nesting blueprints like this. Is there an alternative way to handle this problem?
推荐答案
不幸的是,嵌套蓝图不是 Flask 中的当前功能.你必须手动完成.您可能可以编写适用于您的特定情况的代码,但 Flask 尚未添加通用解决方案.有一些关于问题跟踪器的讨论:
Unfortunately, nested blueprints are not a current feature in Flask. You'll have to do it manually. You could probably code something that works for your specific case, but a general solution has not been added to Flask. There has been some discussion on the issue tracker:
- https://github.com/mitsuhiko/flask/issues/593
- https://github.com/mitsuhiko/flask/issues/1548
- https://github.com/pallets/flask/issues/3215
将可嵌套的蓝图添加到 Flask 中并不像自动为路由添加前缀那么简单.嵌套时需要考虑蓝图的许多其他功能,这使得一般实现变得更加复杂.尚未实施的原因是社区中没有人对它有足够大的需求,而不是通过快速解决方法与提供一般实施来解决.
Adding nestable blueprints into Flask is not as trivial as automatically appending a prefix to routes. There are many other features of blueprints that need to be considered when nesting that make a general implementation significantly more complicated. The reason this has not been implemented yet is that no one in the community has had a great enough need for it that wasn't solved by a quick workaround vs contributing a general implementation.
这篇关于Flask 中的嵌套蓝图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!