什么会导致 Django 模板 for 循环引发关键错误?
我将一个正在运行的 Django 应用程序升级到 1.1,现在在 for 循环中出现 KeyError 异常!
模板错误
In template /vol/.../templates/base_bbn.html, error at line 7
Caught an exception while rendering: 'django.contrib.comments.urls.'
您可能会认为像这样的 for 循环上不可能出现 KeyError,因为它迭代的每个项目都有一个键。
{% block blog_class %}
{% for post in POSTS %} # <-----------Template error on this line
<p class="bbn-dateln">{{ post.publish|date:"Y F d" }
实际的异常是 KeyError
File "/usr/lib/python2.5/site-packages/django/utils/importlib.py", line 36, in import_module
return sys.modules[name]
KeyError: 'django.contrib.comments.urls.'
关于如何调试它有什么建议吗?我传递给此模板进行渲染的帖子看起来不错......
I upgraded a working Django app to 1.1 and I now get a KeyError exception on a for loop!
Template error
In template /vol/.../templates/base_bbn.html, error at line 7
Caught an exception while rendering: 'django.contrib.comments.urls.'
You would think that there couldn't be a KeyError on a for loop like this because there would be a key for each item it iterates through.
{% block blog_class %}
{% for post in POSTS %} # <-----------Template error on this line
<p class="bbn-dateln">{{ post.publish|date:"Y F d" }
The actual exception is KeyError
File "/usr/lib/python2.5/site-packages/django/utils/importlib.py", line 36, in import_module
return sys.modules[name]
KeyError: 'django.contrib.comments.urls.'
Any suggestions on how to debug this? POSTS that I'm passing to this template to be rendered looks fine....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的!我自己想出来了。
看来 1.1 中的 django.contrib.comments.urls 现在依赖于模块 dateutils,而我的 python 环境没有。当我通过 easy_install 安装 dateutils 后,它又开始工作了。
由于某种原因,这导致的异常在 django 错误页面上以“模板错误”和“KeyError”的形式出现。在python调试器中花了一些时间才找到真正的原因
Ok! I figured it out myself.
It seems that django.contrib.comments.urls in 1.1 now has a dependency on module dateutils that my python environment didn't have. Once I installed dateutils via easy_install, it started working again.
For some reason, the exception this caused bubbled up as a "template error" and "KeyError" on the django error page. It took some time in the python debugger to find the real cause