Robert Brax Robert Brax - 1 year ago 99
Python Question

How to organize one Blueprint with multiple routes files

I got Blueprint working properly.

App structure:


In I register the blueprint:

app.register_blueprint( users, url_prefix = '/users' )

And in users/ I create it:

users = Blueprint( 'users', __name__, template_folder = "usersViews" )
@users.route( '/registration', methods = [ 'GET' ] )
def get_register_user_form( ):
# Code......

What I need to do is add other files into users/ from which I need to use @users.route like in:



But this won't work as the blueprint is only created in the original users/ I'm not sure the proper way to handle this case ? I guess recreating the blueprint in each route file with users = Blueprint( 'users', name, template_folder = "usersViews" ) is not the proper way to do so. So how could I achieve that ?

Answer Source

I would split some of this out into files like this:

app structure: (main app)
users/ (for blueprint)

then, in the main setup your blueprints:

app = Flask(__name__)

from .users import users as user_blueprint
app.register_blueprint(user_blueprint, url_prefix='/users')

return app

now, in your users/ something like this:

from flask import Blueprint, url_for

users = Blueprint('users', __name__)

from . import routes, routes2, routes3

Then in users/, users/, etc:

from . import users

Caveat: I've never actually done this! But this is the pattern I use for Flask blueprints and it seemed like it would solve your problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download