Hi all. I'm just getting started with Flask. One thing I can't figure out is how I can put my views in a views package/module. It seems like all of the views must be defined in the same module in which the app object is instantiated, because in order to realize the view functions the app module must import the views module but in order to use the @app.route decorator, the views module must import the app object from the app module. Am I missing something? Drew Vogel
> Hi all. I'm just getting started with Flask. One thing I can't figure > out is how I can put my views in a views package/module. It seems like > all of the views must be defined in the same module in which the app > object is instantiated, because in order to realize the view functions > the app module must import the views module but in order to use the > @app.route decorator, the views module must import the app object from > the app module. Am I missing something? http://flask.pocoo.org/docs/patterns/packages/ # app/mods/pages/__init__.py from flask import Module, render_template mod = Module('app.mods.pages') # Actually not sure what to pass here @mod.route('/about/') def about(): render_template('pages/about.html') # app/mods/pages/templates/about.html <h1>Welcome!</h1> # app/__init__.py from flask import Flask from app.mods import pages app = Flask('app') app.register_module(pages.mod, url_prefix='/page') # /page/about/ should now say <h1>Welcome!</h1>
That is very clear. Thank you! On Tue, Aug 24, 2010 at 3:20 PM, Dag Odenhall <dag.odenhall@gmail.com>wrote: > > > Hi all. I'm just getting started with Flask. One thing I can't figure > > out is how I can put my views in a views package/module. It seems like > > all of the views must be defined in the same module in which the app > > object is instantiated, because in order to realize the view functions > > the app module must import the views module but in order to use the > > @app.route decorator, the views module must import the app object from > > the app module. Am I missing something? > > http://flask.pocoo.org/docs/patterns/packages/ > > > # app/mods/pages/__init__.py > from flask import Module, render_template > > mod = Module('app.mods.pages') # Actually not sure what to pass here > > @mod.route('/about/') > def about(): > render_template('pages/about.html') > > # app/mods/pages/templates/about.html > <h1>Welcome!</h1> > > # app/__init__.py > from flask import Flask > > from app.mods import pages > > app = Flask('app') > app.register_module(pages.mod, url_prefix='/page') > > # /page/about/ should now say <h1>Welcome!</h1> > >