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?
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():
...
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.
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]+');
});
});
and routes can be gotten like action('Blueprint1PhotoController@index')
.
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?
See Question&Answers more detail:
os