Getting a request parameter in Jinja2

Shankar Cabus picture Shankar Cabus · Mar 10, 2012 · Viewed 37.9k times · Source

How can I retrieve a request param a in Jinja2 template?

http://foo.bar?a=1

Answer

Kalle picture Kalle · Jun 1, 2013

I'm a bit late with this answer, but the other solutions don't really account for your use of Flask.

The fact that you're using Flask with Jinja2 makes your situation a bit different from other frameworks. Flask actually makes some global variables available to you in all Jinja2 templates without requiring you to pass them to the template explicitly.

To quote a part of the Flask documentation at http://flask.pocoo.org/docs/templating/#standard-context:

The following global variables are available within Jinja2 templates by default:

...

request The current request object (flask.request)

...

So for example to show the request parameter 'a' in the template:

{{ request.args.get('a') }}

The documentation link also lists the other global variables you can access in a similar way.