更新:我提出的MainHandler到app.py文件,并删除from handlers import MainHandler
线和它的作品。所以显然这与我对如何从其他文件中提取代码缺乏理解有关。得到错误我不明白当定义模板配置的龙卷风
我是新来的Python,这意味着我也新旋风。我有我认为是最简单的龙卷风应用程序。当我在做以下事情:
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, World")
事情工作正常。我现在修改了我的应用程序配置以使用模板。这里是我的app.py:
import tornado.ioloop
import tornado.web
import os.path
from handlers import MainHandler
application = tornado.web.Application(
[
(r"/", MainHandler)
],
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static")
)
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
这里是MainHandler:
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
下面是index.html的:
<!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"><!--<![endif]-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Index Page</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
当我尝试并获得http://localhost:8888/
我正在下面的错误,这对我来说,现在是相当神秘:
ERROR:tornado.application:Uncaught exception, closing connection.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/iostream.py", line 341, in wrapper
callback(*args)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "<string>", line 3, in raise_exc_info
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/httpserver.py", line 327, in _on_headers
self.request_callback(self._request)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/web.py", line 1567, in __call__
handler = spec.handler_class(self, request, **spec.kwargs)
TypeError: 'module' object is not callable
ERROR:tornado.application:Exception in callback functools.partial(<function wrap.<locals>.wrapped at 0x104b78d40>)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/ioloop.py", line 458, in _run_callback
callback()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "<string>", line 3, in raise_exc_info
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/iostream.py", line 341, in wrapper
callback(*args)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "<string>", line 3, in raise_exc_info
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/httpserver.py", line 327, in _on_headers
self.request_callback(self._request)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/tornado/web.py", line 1567, in __call__
handler = spec.handler_class(self, request, **spec.kwargs)
TypeError: 'module' object is not callable
你的代码运行正常在我的机器上使用Python 2 – avi
那么,什么是index.html中? –
@ColeMaclean我在我的问题中加入了这个。谢谢! – Gregg