我仍然是Flask的新手,因此可能有一种明显的方法可以实现这一目标,但到目前为止我还没有能够从文档中找到它.我的应用程序分为几个主要是不同的部分,分享用户/会话/安全性和基本模板等所有内容,但大多数情况下不会进行很多交互,应该在不同的路径下进行路由/part1/....我认为这正是蓝图的用武之地.但是如果我需要在蓝图下进一步分组路由和逻辑呢?
例如,我有blueprint1,url_prefix='/blueprint1'也许在那之下我希望有一组视图围绕用户共享照片和其他用户评论它们.我想不出比这更好的做法:
# 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():
...
Run Code Online (Sandbox Code Playgroud)
这里的问题是所有与照片部分相关的视图blueprint1都位于"顶层",右侧可能有视频或音频或其他任何(命名为videos_index()......)的蓝图.有没有办法以更分层的方式对它们进行分组,比如模板如何在'blueprint1/photos'子目录下?当然,我可以将所有照片视图放在他们自己的模块中,以使它们分开组织,但是如果我想将父'blueprint1/photos'路径更改为其他内容呢?我确信我可以创建一个在相同根路径下对相关路由进行分组的函数或装饰器,但是我仍然必须使用photos_前缀命名所有函数并引用它们url_for('blueprint1.photos_show') 当Flask应用程序变大并且您需要将相似的部分组合在一起时,蓝图就好了,但是当蓝图本身变大时,您无法做同样的事情.
作为参考,在Laravel中,您可以Controller在视图是方法的类下对相关的"视图"进行分组.控制器可以驻留在分层名称空间中,例如app\Http\Controllers\Blueprint1\Photocontroller,路由可以组合在一起
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', …Run Code Online (Sandbox Code Playgroud)