Getting a request parameter in Jinja2

后端 未结 4 1406
猫巷女王i
猫巷女王i 2021-01-31 15:53

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

http://foo.bar?a=1
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 16:23

    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.

提交回复
热议问题