Frage

I am playing around with tornado .Need Help.

Now my browser have a cookie set with id="chatdemo_user" and its value as "gaurav". So, when I hit URL http://localhost:9999 ,then get_current_user function in BaseHandler class gets the user,but when tornado renders index.html template ,for some reason,it doesn't get the value of current_user["name"] and I get error TypeError: string indices must be integers, not str. Can anybody help me with it ?

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>

ERROR

[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
War es hilfreich?

Lösung

Replace current_user["name"] with simply current_user in your template. The current_user value returned from get_current_user is a string.

Andere Tipps

the best way is to decode the 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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top