2014-01-09 14 views
1

更新:我提出的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 
+0

你的代码运行正常在我的机器上使用Python 2 – avi

+0

那么,什么是index.html中? –

+0

@ColeMaclean我在我的问题中加入了这个。谢谢! – Gregg

回答

1

好吧,经过大量的谷歌搜索,我已经找到了我的问题。

首先,我不得不添加__init__.py到处理程序文件夹,这样蟒蛇认识到这是一个包。这看起来很随意,所以我必须在后面阅读这些逻辑。

其次,我必须修改导入到from handlers.MainHandler import *,甚至认为我最想做的是from handlers import *,这样我就可以一次导入所有的处理程序。现在,我会解决什么是工作。

+1

在Python中,模块的文件名是其完全限定名称的一部分,因此如果将MainHandler类放入同名文件中,则该类为handlers.MainHandler.MainHandler。 '从X导入*'不会自动查找所有可用文件;你必须单独导入它们。如果你在'__init __。py'中放入了'import mainHandler'(对于你所有的其他处理程序都是这样),它将按照你期望的方式工作。 –

+0

好吧,从其他一些阅读中我说他们说最好的做法是做绝对进口而不是进口。这很好。来自Java世界的做法是一样的。所以我可能会修改。但是你的解释很有帮助,所以谢谢。 – Gregg

+1

另一个捷径:对于定义处理程序的特定情况,您可以使用(完全限定)名称而不导入任何内容:'(r'/','handlers.MainHandler.MainHandler')'。应用程序将自动导入这些类。 –