更新:我将MainHandler移动到app.py文件中并删除from handlers import MainHandler
行并且它可以正常工作。显然,这与我对如何从其他文件中提取代码缺乏了解有关。
我是Python的新手,这意味着我也是Tornado的新手。我认为最简单的龙卷风应用程序。当我做以下事情时:
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
答案 0 :(得分:1)
好吧,经过大量的谷歌搜索后,我发现了我的问题。
首先,我必须将__init__.py
添加到处理程序文件夹中,以便python将其识别为包。这看起来很随意,所以我稍后会读到它的逻辑。
其次,我必须将导入修改为from handlers.MainHandler import *
,甚至认为我最理想的是from handlers import *
,这样我就可以立即导入所有处理程序。现在,我将解决正在发挥作用的问题。