在 Tornado 中处理未定义的模板变量 [英] Handling an undefined template variable in Tornado

查看:36
本文介绍了在 Tornado 中处理未定义的模板变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个龙卷风模板(例如,在文件 logout.html 中)我在注销过程中呈现错误:

This is a tornado template (say, in the file logout.html) I render on an error in the logout process:

  {% if logout_error %}
    Oops! The logout failed. Please close all open documents and try again
  {% end %}

这可以使用

self.render("logout.html", logout_error=True)

如果注销成功,我必须这样做

If the logout is successful, I have to do

self.render("logout.html", logout_error=False)

如果我忽略 logout_error=False,我得到

If I ignore logout_error=False, I get

NameError: global name 'logout_error' is not defined

如果有很多标志(错误的),关键字参数会堆积起来.如果 logout_error 不存在,我是否可以要求模板将其视为 False ?

If there are a lot of flags (which are false), the keyword arguments can pile up. Is there a way I can ask the template to consider logout_error as False if it does not exist?

推荐答案

hacking 使用 locals().get() 是一种方法.另一个更正统的做法是使用 try.Tornado 模板支持它,因此您可以:

Hacking around using locals().get() is one way to do it. Another, bit more orthodox is using try. Tornado template supports it, so you can:

{% try %}
  {% if logout_error %}
    Oops! The logout failed. Please close all open documents and try again
  {% end %}
{% except %}
{% end %}

这篇关于在 Tornado 中处理未定义的模板变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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