简体   繁体   English

是什么会导致Django模板for循环引发键错误?

[英]What would cause a Django template for loop to raise a Key Error?

I upgraded a working Django app to 1.1 and I now get a KeyError exception on a for loop! 我将可运行的Django应用程序升级到1.1,现在在for循环上收到KeyError异常!

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. 您会认为在这样的for循环上不会出现KeyError,因为迭代的每个项目都会有一个键。

{% 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 实际的异常是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.... 我传递给此模板以进行呈现的帖子看起来不错。...

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. 看来1.1版中的django.contrib.comments.urls现在对我的Python环境没有的dateutils模块有依赖性。 Once I installed dateutils via easy_install, it started working again. 通过easy_install安装dateutils后,它又开始工作。

For some reason, the exception this caused bubbled up as a "template error" and "KeyError" on the django error page. 出于某种原因,这导致的异常在django错误页面上冒泡为“模板错误”和“ KeyError”。 It took some time in the python debugger to find the real cause 在python调试器中花了一些时间才能找到真正的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM