librelist archives

« back to archive

Project organization

Project organization

From:
Drew Vogel
Date:
2010-08-24 @ 19:20
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

Re: [flask] Project organization

From:
Dag Odenhall
Date:
2010-08-24 @ 20:20
> 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>

Re: [flask] Project organization

From:
Drew Vogel
Date:
2010-08-24 @ 21:01
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>
>
>