librelist archives

« back to archive

Using context processors when using app factories

Using context processors when using app factories

From:
John Anderson
Date:
2011-02-07 @ 07:26
My directory structure is like this:
project/
-> context_processors.py
-> main.py
->/apps/
--->__init__.py
--->/app1
-----> views.py
-----> models.py
--->/app2

I have this app_factory:

def create_app(config_filename='settings.py'):
    app = Flask(__name__)
    app.config.from_pyfile(config_filename)

    from project.apps.events.views import events
    from project.context_processors import context_processors

    app.register_module(context_processors)
    app.register_module(events)

    import regolution.context_processors

    return app

The context processors code:
from google.appengine.api import users
from flask import Module

context_processors = Module(__name__)

@context_processors.context_processor
def inject_logout_url():
    return { 'create_logout_url' : users.create_logout_url }

@context_processors.context_processor
def inject_current_user():
    return { 'current_user' : users.get_current_user() }


I'm just trying to get these values registered on *all* my views, so I can
use them whenever but neither one is available.  Any ideas on what I'm doing
wrong?

Re: [flask] Using context processors when using app factories

From:
Simon Sapin
Date:
2011-02-07 @ 08:32
Le 07/02/2011 16:26, John Anderson a écrit :
> Any ideas on what I'm doing wrong?
>

After the line

     import regolution.context_processors

Try adding

     app.register_module(regolution.context_processors.context_processors)

ie. register the Module object with the app.

Regards,
-- 
Simon Sapin
http://exyr.org/

Re: [flask] Using context processors when using app factories

From:
John Anderson
Date:
2011-02-07 @ 08:40
>
>
> After the line
>
>     import regolution.context_processors
>
> Try adding
>
>     app.register_module(regolution.context_processors.context_processors)
>
> ie. register the Module object with the app.
>
>
Thats what I'm doing, if you look at the sample, I'm grabbing the module
object:
from project.context_processors import context_processors

then i'm registering it:
app.register_module(context_processors)

Re: [flask] Using context processors when using app factories

From:
John Anderson
Date:
2011-02-07 @ 08:56
>
>
>> Thats what I'm doing, if you look at the sample, I'm grabbing the module
> object:
> from project.context_processors import context_processors
>
> then i'm registering it:
> app.register_module(context_processors)
>

using @ctx.app_context_processor   Since my context_processors.py were their
own module I don't think any of templates for other modules had access to it
:)