django'module'对象没有属性'call_command'

时间:2010-08-09 02:04:38

标签: python django

我写了一个像

这样的函数
def cmd_run(host="localhost", port="8000"):
    """Run server at given host port (or localhost 8000)"""
    from django.core import management
    host_port = '%s:%s' % (host, port)
    management.call_command('runserver', host_port)

当我执行它时,抛出异常:

Traceback (most recent call last):
  File "/home/leona/workspace/bettyskitchen/lib/djangocliutils/bin/djangoctl", line 8, in <module> cli(*sys.argv[1:])
  File "/home/leona/workspace/bettyskitchen/lib/djangocliutils/cmds.py", line 119, in  __call__ 
print method(*args) or ""
  File "/home/leona/workspace/bettyskitchen/lib/djangocliutils/cmds.py", line 170, in __call__
    return self.method(*args, **kw)
  File "/home/leona/workspace/bettyskitchen/lib/djangocliutils/djangocmds/basic.py", line 19, in cmd_run
    management.call_command('runserver', host_port)
AttributeError: 'module' object has no attribute 'call_command'

我该如何解决?

2 个答案:

答案 0 :(得分:0)

嗯,这里有效......也许你的django版本没有这个功能?在托管shell python manage.py shell中尝试一下,然后尝试help(management)查看您的版本是否存在。 另一种可能是在__init__.py目录中导致django.core.management函数的call_command文件(其中定义了call_command)的损坏或修改。

答案 1 :(得分:0)

这个问题解决了,我用的django是0.96不支持call_command函数,所以我改成了:

def cmd_run(host="localhost", port="8000"):
    """Run server at given host port (or localhost 8000)"""
    from django.core import management
    management.runserver(host, port)

这次它有效!