2013-04-26 108 views
1
class MainHandler(BaseHandler): 
    @tornado.web.authenticated 
    def get(self): 
     self.render("index.html", messages=MessageMixin.cache) 

所以MainHandler没有通过requestcurrent_userindex.html。但在index.html我试过<p>{{ current_user }}</p> <p>{{ request }}</p>,然后有很多输出产生。那么这是龙卷风中的一种'全局变量'吗?龙卷风 - 龙卷风中的'全局变量'?

回答

4

在龙卷风模板中免费赠送给你几件东西。

这些变量不需要传入 - 这就是您用current_user和请求看到的。

这里是你默认

+0

它有很大帮助!谢谢 ! – CDT 2013-04-26 14:18:34

0

他们在龙卷风默认模板上下文的一部分得到变量的list。该文件实际上涵盖了所有的available ones

0
  • 秘密在源代码!

  • tornado.web有一个名为 'get_template_namespace' 功能,你甚至可以覆盖

  • 代码细节:

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