gpt4 book ai didi

python - Tornado 模板未获取 current_user ["name"的值]

转载 作者:行者123 更新时间:2023-12-01 05:21:21 24 4
gpt4 key购买 nike

我正在玩 Tornado 。需要帮助。

现在我的浏览器有一个 id="chatdemo_user"的 cookie,其值为“gaurav”。因此,当我点击 URL http://localhost:9999 时,BaseHandler 类中的 get_current_user 函数会获取用户,但是当tornado 渲染 index.html 模板时,由于某种原因,它没有获取 current_user["name"] 的值,并且收到错误 TypeError: stringindexs must be integers, not str。有人可以帮我吗?

Tornado 代码

define("port", default=9999, help="run on the given port", type=int)

class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
user = self.get_cookie("chatdemo_user")
if not user: return None
return user

class MainHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
self.render("index.html")

def main():
parse_command_line()
app = tornado.web.Application(
[
(r"/", MainHandler),
],
cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
login_url="http://localhost:8080/login",
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookies=True,
)
app.listen(options.port)
tornado.ioloop.IOLoop.instance().start()


if __name__ == "__main__":
main()

模板

<html>
<body>
<b>{{ current_user["name"] }}</b>
</body>
</html>

错误

[E 140307 17:03:09 web:1305] Uncaught exception GET / (10.19.122.127)
HTTPRequest(protocol='http', host='localhost:9999', method='GET', uri='/', version='HTTP/1.1', remote_ip='10.19.122.127', headers={'Accept-Language': 'en-US,en;q=0.8', 'Accept-Encoding': 'gzip,deflate,sdch', 'Host': 'indlin1564:9999', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36', 'Connection': 'keep-alive', 'Cookie': 'csrftoken=zf1DA5uT5yNb0mcrWnP6poOCw02SB5ie; chatdemo_user=gaurav; sessionid=w06k4g7qwm9z98fp39rfscek6p5r3qwu', 'Cache-Control': 'max-age=0'})
Traceback (most recent call last):
File "/optusr1/aimsys/tools/gauravy/local/lib/python2.7/site-packages/tornado/web.py", line 1218, in _when_complete
callback()
File "/optusr1/aimsys/tools/gauravy/local/lib/python2.7/site-packages/tornado/web.py", line 1239, in _execute_method
self._when_complete(method(*self.path_args, **self.path_kwargs),
File "/optusr1/aimsys/tools/gauravy/local/lib/python2.7/site-packages/tornado/web.py", line 2395, in wrapper
return method(self, *args, **kwargs)
File "./chatdemo.py", line 81, in get
self.render("index.html")
File "/optusr1/aimsys/tools/gauravy/local/lib/python2.7/site-packages/tornado/web.py", line 615, in render
html = self.render_string(template_name, **kwargs)
File "/optusr1/aimsys/tools/gauravy/local/lib/python2.7/site-packages/tornado/web.py", line 722, in render_string
return t.generate(**namespace)
File "/optusr1/aimsys/tools/gauravy/local/lib/python2.7/site-packages/tornado/template.py", line 278, in generate
return execute()
File "index_html.generated.py", line 11, in _tt_execute
_tt_tmp = current_user["name"] # index.html:3
TypeError: string indices must be integers, not str

最佳答案

在模板中将 current_user["name"] 替换为 current_user。从 get_current_user 返回的 current_user 值是一个字符串。

关于python - Tornado 模板未获取 current_user ["name"的值],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22249406/

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