2013-04-26 4 views
1
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>, а затем появилось много выходных данных. Так это какая-то «глобальная переменная» в «Торнадо»?Торнадо - «Глобальные переменные» в торнадо?

ответ

4

Несколько вещей даны вам бесплатно в шаблонах Tornado.

Эти переменные не должны быть переданы - это то, что вы видите это с помощью current_user и запроса.

Вот list всех переменных, которые вы получаете по умолчанию

+0

Это очень помогает! Благодаря ! – CDT

0

Они являются частью контекста шаблона по умолчанию в Торнадо. Документация фактически охватывает все 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 

 Смежные вопросы

  • Нет связанных вопросов^_^