Python django';模块';对象没有属性';调用#u命令';

Python django';模块';对象没有属性';调用#u命令';,python,django,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) 当我执行

我写了一个函数,比如

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'
回溯(最近一次呼叫最后一次):
文件“/home/leona/workspace/bettyskeet/lib/djangocliutils/bin/djangoctl”,第8行,在cli(*sys.argv[1:])中
文件“/home/leona/workspace/bettyskeet/lib/djangocliutils/cmds.py”,第119行,在
打印方法(*args)或“”
文件“/home/leona/workspace/bettyskeet/lib/djangocliutils/cmds.py”,第170行,在调用中__
返回自方法(*参数,**kw)
文件“/home/leona/workspace/bettyskeet/lib/djangocliutils/djangocmds/basic.py”,第19行,在cmd_run中
管理。调用\u命令('runserver',主机\u端口)
AttributeError:“模块”对象没有“调用命令”属性

我怎样才能修好它呢?

好吧,它在这里工作。。。也许你的django版本没有这个功能?在托管shell
python manage.py shell
中尝试它,并尝试
help(management)
查看您的版本中是否有它。
另一种可能是
django.core.management
目录中的
\uuuuu init\uuuuuuuuuuupy
文件(定义了call\u命令)损坏或修改,没有导入
call\u命令
函数。

这个问题得到解决,我使用的django是0.96,不支持call\u命令函数,所以我改为:

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)

这一次成功了

我不知道你使用0.96的原因,但我建议如果可能的话升级到新版本。