gpt4 book ai didi

python - 如何将值传递给 Tornado 中的模板

转载 作者:太空狗 更新时间:2023-10-30 00:36:59 26 4
gpt4 key购买 nike

我有一个模板,它显示了很多从服务器传递过来的值,我的问题是如何将这些值传递给模板文件。我的处理程序代码如下: AdminHandler 类(tornado.web.RequestHandler): def get(self, *args, **kwargs): #响应一个get方法 #self.write("AdminHandler::Inside GET 函数")

    userName = "Alwin Doss"
welcomeMessage = "Good evening are you enjoying kids dance"
items = {}
items["userName"] = userName
items["welcomeMessage"] = welcomeMessage


self.render("web/admin.html", title="Admin Page", items=items)

而我的模板代码如下: {% items['userName'] %} {% items['welcomeMessage'] %} {% 结束 %}

问题是我无法访问模板文件中的这些值。我收到以下错误:

raise ParseError("未知运算符: %r"% 运算符)ParseError:未知运算符:“项目[‘用户名’]”错误:root:500 GET/admin (127.0.0.1) 3.27ms

最佳答案

这是一个类似于您似乎正在做的演示。查看模板的语法,了解 {% %}{{ }} block 的不同用法。这段代码:

from tornado import template

t = template.Template('''\
{% for user in users %}
{{ user['userName'] }}
{{ user['welcomeMessage'] }}
{% end %}
''')

# create first user and append to a user list
users = []
user = { "userName" : "Alwin Doss",
"welcomeMessage" : "Good evening are you enjoying kids dance"}
users.append(user)

# create and append second user
user = { "userName" : "John Smith",
"welcomeMessage" : "Good evening, JS"}
users.append(user)

# render the template and output to console
print t.generate(users = users)

产生这个输出:

Alwin Doss 
Good evening are you enjoying kids dance

John Smith
Good evening, JS

有关 Tornado 模板的更多信息,请查看 this tutorial当然还有 Tornado templates documentation .

关于python - 如何将值传递给 Tornado 中的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10461585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com