首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tornado -- Tornado中的“全局变量”?

tornado -- Tornado中的“全局变量”?
EN

Stack Overflow用户
提问于 2013-04-26 18:16:58
回答 3查看 2.5K关注 0票数 1
代码语言:javascript
复制
class MainHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        self.render("index.html", messages=MessageMixin.cache)

因此,MainHandler不会将requestcurrent_user传递给index.html。但在index.html中,我尝试了<p>{{ current_user }}</p> <p>{{ request }}</p>,然后生成了很多输出。那么这是Tornado中的某种“全局变量”吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-04-26 20:53:20

在Tornado模板中有几样东西是免费提供给你的。

这些变量不需要传入-这就是您在current_user和request中看到的情况。

下面是您默认获得的所有变量的list

票数 3
EN

Stack Overflow用户

发布于 2016-12-23 16:15:16

get模板的秘密在源代码中code!

  • tornado.web有一个名为‘_
  • _
  • ’的函数,你甚至可以覆盖
  • 代码细节:

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2014-12-17 04:39:13

它们是Tornado中默认模板上下文的一部分。该文档实际上涵盖了所有available ones

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16234033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档