Context processor use is problematic. #153

Closed
skorokithakis opened this Issue Jan 3, 2011 · 6 comments

Comments

Projects
None yet
5 participants

Implementing Facebook authentication, I need a context processor that will insert a current_user variable to the template context by performing some operations on the cookies/request. Since context processors need the app object for the decorator, they are rather hard to put in another file, and this feels unclean.

Could something be done to enable putting context processors in a context_processors.py file by themselves, short of importing and decorating them one by one?

Contributor

fsouza commented Jan 3, 2011

Please take a look here: https://github.com/franciscosouza/labs/blob/master/labs/context_processors.py

Just append to your __init__.py (or wherever you defined your application):

import context_processors

as I did here: https://github.com/franciscosouza/labs/blob/master/labs/__init__.py.

You can also use Flask current_app object on your context_processor.py module: http://flask.pocoo.org/docs/api/#flask.current_app.

Ah, thank you. Can we get this bit of info added to the context processor docs? It will help many people, I think.

EDIT: Using current_app doesn't seem to work, however importing app directly works, without the circular import I would have expected.

Owner

mitsuhiko commented Jan 3, 2011

current_app only works when a request is active. That's a side effect or how Python works internally which I don't want to hack around.

Contributor

fsouza commented Jan 3, 2011

Ops, my wrong :)

That's a general question on how to structure bigger apps and split them into files. There are dozens of articles and examples (so called skeletons of the projects) on the subject. There are even some in official Flask docs in the patterns chapter.

So I say we can close this issue.

Contributor

DasIch commented Jun 20, 2013

Agreed.

DasIch closed this Jun 20, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment