我正在玩龙卷风。需要帮助。
现在我的浏览器设置了一个id =“chatdemo_user”的cookie,其值为“gaurav”。因此,当我点击URL http://localhost:9999
时,BaseHandler类中的get_current_user函数会获取用户,但是当tornado呈现index.html
模板时,由于某种原因,它不会获得current_user["name"]
的值我收到错误TypeError: string indices must be integers, not str
。有人可以帮我吗?
TORNADO CODE
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()
TEMPLATE
<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
答案 0 :(得分:2)
在模板中使用current_user["name"]
替换current_user
。 current_user
返回的get_current_user
值是一个字符串。
答案 1 :(得分:0)
最好的方法是解码cookie
def get_current_user(self):
user_json = self.get_secure_cookie("chatdemo_user")
if user_json:
return tornado.escape.json_decode(user_json)
else:
return None