0

I'm developing a Django project and deploying it to OpenShift PaaS. At first everything worked, but after some changes irrelevant to template system (I added django-hosts) something broke there and a "module object 'template' has no attribute 'loader'" error started to appear. What was even stranger, it appeared only two times after each wsgi app restart, and on 3rd request everything started to work. I went back to last commit before breakage, but the problem persisted. I recreated project from scratch and reinstalled my Django app, but it didn't go either; error started appearing every time, not just with first 2 requests. from django import template really imports template module object, but this object lacks about 5 attributes, including loader, as compared to what expected.

Then I noticed that same thing happens if I try to run the same code from Django shell locally. But it still works in my app's views.py with local Django development server. And in used to work in OpenShift initially. I tried replacing from django import template with from django.template import loader and calling loader directly - and EVERYTHING WORKED

I think I don't understand something about Python import. What's the difference between

import a
a.b

and

from a import b
b

?

Why can a.b in first example miss attributes b has in second one?

4

1 回答 1

1

发生这种情况是因为 template 是 django 中的一个包,而 loader 是一个模块,而您期望模板是一个模块,而 loader 是它的属性之一。它按预期工作。

于 2012-09-04T06:16:09.130 回答