【问题标题】:Pylons mako templates: debugging "Internal Server Error" / "NoneType" errorsPylons mako 模板:调试“内部服务器错误”/“NoneType”错误
【发布时间】:2011-06-23 01:26:03
【问题描述】:

我在使用 Pylons 0.9.7 的 Mako 模板中经常遇到这个错误:

AttributeError: 'NoneType' object has no attribute 'decode'

通常我输入了一个不存在的变量名,尝试在代码行中使用 linbebreak,或者其他一些小错误。绝对是我的错。

这会导致浏览器中出现“内部服务器错误”,在调试视图中出现同样的情况,并且堆栈跟踪从 HTTPServer 开始并以 mako/exceptions.py 中的 AttributeError 结束。

我可以做些什么来使调试更容易,比如找出在 Mako 模板中生成异常的行?谢谢!

【问题讨论】:

  • 完整的堆栈跟踪肯定会有所帮助。如果您能够将其追踪到模板中的一行,那就更好了。

标签: pylons mako


【解决方案1】:

我不确定这是否是同一个问题,但据我所知,当您执行 AJAX 加载页面片段时,这种情况经常发生。那么你不会得到比这条消息更有用的东西了。

但是,如果您尝试在浏览器中加载 AJAX 请求本身的地址(如果需要,将 post 参数替换为 get 参数),您将获得一个“正常”的调试页面。

【讨论】:

  • 感谢 Loxs,这是有用的建议。不是我的特殊问题,但很高兴记住。在这种情况下,您还可以在控制台上查找调试 URL,然后将其粘贴到浏览器中以使用出色的交互式调试器。
【解决方案2】:

在我的例子中,我的模板中有一个除以 0 的错误。这导致了内部服务器错误,并且控制台输出中的堆栈跟踪非常无用。

我知道,听起来我不应该在模板中包含这种逻辑,但在这种情况下,我认为这样做是有道理的。这是我除以 0 得到的堆栈跟踪:

Exception happened during processing of request from ('127.0.0.1', 50681)
Traceback (most recent call last):
  File "/Library/Python/2.6/site-packages/Paste-1.7.4-py2.6.egg/paste/httpserver.py", line 1068, in process_request_in_thread
    self.finish_request(request, client_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/SocketServer.py", line 320, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/SocketServer.py", line 615, in __init__
    self.handle()
  File "/Library/Python/2.6/site-packages/Paste-1.7.4-py2.6.egg/paste/httpserver.py", line 442, in handle
    BaseHTTPRequestHandler.handle(self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/BaseHTTPServer.py", line 329, in handle
    self.handle_one_request()
  File "/Library/Python/2.6/site-packages/Paste-1.7.4-py2.6.egg/paste/httpserver.py", line 437, in handle_one_request
    self.wsgi_execute()
  File "/Library/Python/2.6/site-packages/Paste-1.7.4-py2.6.egg/paste/httpserver.py", line 287, in wsgi_execute
    self.wsgi_start_response)
  File "/Library/Python/2.6/site-packages/Paste-1.7.4-py2.6.egg/paste/cascade.py", line 130, in __call__
    return self.apps[-1](environ, start_response)
  File "/Library/Python/2.6/site-packages/Paste-1.7.4-py2.6.egg/paste/registry.py", line 375, in __call__
    app_iter = self.application(environ, start_response)
  File "/Library/Python/2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/middleware.py", line 201, in __call__
    self.app, environ, catch_exc_info=True)
  File "/Library/Python/2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/util.py", line 94, in call_wsgi_application
    app_iter = application(environ, start_response)
  File "/Library/Python/2.6/site-packages/WebError-0.10.2-py2.6.egg/weberror/evalexception.py", line 235, in __call__
    return self.respond(environ, start_response)
  File "/Library/Python/2.6/site-packages/WebError-0.10.2-py2.6.egg/weberror/evalexception.py", line 483, in respond
    return debug_info.content()
  File "/Library/Python/2.6/site-packages/WebError-0.10.2-py2.6.egg/weberror/evalexception.py", line 545, in content
    result = tmpl_formatter(self.exc_value)
  File "/Library/Python/2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/error.py", line 43, in mako_html_data
    css=False)
  File "/Library/Python/2.6/site-packages/Mako-0.3.2-py2.6.egg/mako/template.py", line 189, in render
    return runtime._render(self, self.callable_, args, data)
  File "/Library/Python/2.6/site-packages/Mako-0.3.2-py2.6.egg/mako/runtime.py", line 403, in _render
    _render_context(template, callable_, context, *args, **_kwargs_for_callable(callable_, data))
  File "/Library/Python/2.6/site-packages/Mako-0.3.2-py2.6.egg/mako/runtime.py", line 434, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "/Library/Python/2.6/site-packages/Mako-0.3.2-py2.6.egg/mako/runtime.py", line 457, in _exec_template
    callable_(context, *args, **kwargs)
  File "memory:0x1040470d0", line 54, in render_body
  File "/Library/Python/2.6/site-packages/Mako-0.3.2-py2.6.egg/mako/exceptions.py", line 88, in __init__
    self.records = self._init(traceback)
  File "/Library/Python/2.6/site-packages/Mako-0.3.2-py2.6.egg/mako/exceptions.py", line 166, in _init
    line = line.decode('ascii', 'replace')
AttributeError: 'NoneType' object has no attribute 'decode'
----------------------------------------

【讨论】:

    猜你喜欢
    • 2013-02-23
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2023-03-10
    • 2012-06-15
    相关资源
    最近更新 更多