【发布时间】:2014-07-04 12:46:53
【问题描述】:
我正在开发一个简单的 Django 项目。但是,我对 python 和 Django 都不是很熟悉。它不断抛出“NameError”。
在我的模板中,代码如下所示:
{% for habbit_source in habbit_source_list %}
<span class="habbit-hide">{{habbit_source.get_tomato_achieve}}</span>
...
{% endfor %}
get_tomato_achieve是Model类的一个函数:task.models.HabbitSource,定义如下:
def get_tomato_achieve(self):
return rules.get_achieve_tomato(self)
在rules.py中,我想判断get_tomato_achieve(habbit)开头的输入参数的类型,但是我是否使用
if type(habbit) == task.models.HabbitSource:
或
if isinstance(habbit, task.models.HabbitSource):
它会抛出NameError: Name task is not defined.(当然它已经定义了。这是我的应用程序的名称。)
如果我写from task.models import HabbitSource,它会抛出ImportError: cannot import HabbitSource。
我真的不知道如何解决这个问题。
谢谢!
【问题讨论】:
标签: python django python-3.x