在Jinja2中获取请求参数 [英] Getting a request parameter in Jinja2

查看:272
本文介绍了在Jinja2中获取请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Jinja2模板中检索请求参数 a

  http://foo.bar?a=1 


解决方案

<这个答案我迟了一点,但其他的解决方案并没有真正考虑到你对Flask的使用。



事实上你使用的是Flask与Jinja2使你的情况有点不同于其他框架。 Flask实际上使所有Jinja2模板中的某些全局变量可用,而不需要您明确地将它们传递给模板。

引用Flask文档的一部分 http://flask.pocoo.org/docs/templating/#standard-context


以下全局变量默认在Jinja2模板中可用:

...

请求
当前请求对象(flask.request)




例如在模板中显示请求参数'a':

  {{request.args.get('a')文档链接还列出了其他可以用类似方法访问的全局变量。 


>

p>

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

http://foo.bar?a=1

解决方案

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.

这篇关于在Jinja2中获取请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆