[Flask] Globally accessible variables in Flask

David Lord davidism at gmail.com
Tue Apr 11 09:38:55 EDT 2017


current_user is a LocalProxy. Other examples are current_app, g, request,
and session. You can create your own, it's documented in Werkzeug:
http://werkzeug.pocoo.org/docs/0.11/local/#werkzeug.local.LocalProxy. How
you create it depends on what your data is, but in most cases you probably
want to pass a callable that returns the object.

On Tue, Apr 11, 2017 at 5:39 AM, <badrihippo at gmail.com> wrote:

> I know I figured this out sometime, but can't remember now. How does one
> make a variable that is automatically accessible to all views?
>
> I mean like the current_user variable provided by Flask-Admin: it
> automatically calculates current_user and makes it available to the view
> for each request, without having to manually define it each time.
>
> Basically, make it so that instead of...
>
> @app.route('/example')
> def example():
>     my_var = something()
>
> @app.route('/example/2')
> def second_example():
>     my_var = something()
>
>
> ...it automatically calculates my_var without having to manually define
> it in each view.
>
> Okay, I hope that was clear. And thanks in advance!
>
> —Badri/Hippo
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20170411/e67250a8/attachment.html>


More information about the Flask mailing list