class MainHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
self.render("index.html", messages=MessageMixin.cache)因此,MainHandler不会将request或current_user传递给index.html。但在index.html中,我尝试了<p>{{ current_user }}</p> <p>{{ request }}</p>,然后生成了很多输出。那么这是Tornado中的某种“全局变量”吗?
发布于 2013-04-26 20:53:20
在Tornado模板中有几样东西是免费提供给你的。
这些变量不需要传入-这就是您在current_user和request中看到的情况。
下面是您默认获得的所有变量的list
发布于 2016-12-23 16:15:16
get模板的秘密在源代码中code!
def get_template_namespace(self):
""" Returns a dictionary to be used as the default template namespace.
May be overridden by subclasses to add or modify values.
The results of this method will be combined with additional
defaults in the tornado.template module and keyword arguments
to render or render_string.
"""
namespace = dict(
handler=self,
request=self.request,
current_user=self.current_user,
locale=self.locale,
_=self.locale.translate,
pgettext=self.locale.pgettext,
static_url=self.static_url,
xsrf_form_html=self.xsrf_form_html,
reverse_url=self.reverse_url
)
namespace.update(self.ui)
return namespace
发布于 2014-12-17 04:39:13
它们是Tornado中默认模板上下文的一部分。该文档实际上涵盖了所有available ones
https://stackoverflow.com/questions/16234033
复制相似问题