龙卷风中的断言错误

时间:2013-10-31 12:33:35

标签: python tornado

我编辑了这个问题,以展示一个小例子。这个演示代码来自“龙卷风入门书”,并给出了这个错误:

Traceback (most recent call last):
  File "demo.py", line 25, in <module>
    ui_modules={'Hello', HelloModule}
  File "/usr/local/lib/python2.7/dist-packages/tornado-3.1.1-py2.7.egg/tornado/web.py", line 1422, in __init__
    self._load_ui_modules(settings.get("ui_modules", {}))
  File "/usr/local/lib/python2.7/dist-packages/tornado-3.1.1-py2.7.egg/tornado/web.py", line 1545, in _load_ui_modules
    assert isinstance(modules, dict)
AssertionError

demo.py

import tornado.web
import tornado.httpserver
import tornado.ioloop
import tornado.options 
import os.path
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)


class HelloHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('hello.html')


class HelloModule(tornado.web.UIModule):
    def render(self):
        return '<h1>Hello, world!</h1>'

if __name__ == '__main__':
    tornado.options.parse_command_line()

    app = tornado.web.Application(
        handlers=[(r'/', HelloHandler)],
        template_path=os.path.join(os.path.dirname(__file__), 'templates'),
        ui_modules={'Hello', HelloModule}
    )
    server = tornado.httpserver.HTTPServer(app)
    server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

hello.html的

<html>
<head><title>UI Module Example</title></head>
<body>
{% module Hello() %}
</body>
</html>

2 个答案:

答案 0 :(得分:3)

ui_modules={'Hello', HelloModule}不是dict,而是set

它应该是:ui_modules={'Hello': HelloModule}(注意逗号被冒号取代)。

答案 1 :(得分:0)

实际上这本书的勘误表位于Here