传递设置时,请使用Shell脚本来运行自定义Django Manage.py命令



使用django 1.5

我想在我传递设置时使用shell脚本运行自定义django manage.py命令(即不使用同一目录的设置)。

作为传递设置的一个示例,如果我运行Django开发服务器,我会这样做:

python manage.py runserver ip:port --settings=my.namespace.settings

在与我的manage.py文件相同的目录中,我有我的shell脚本: myscript

MyScript的当前内容:

#!/bin/bash
./manage.py shell --settings=my.namespace.settings

myscript的目录中,运行./myscript确实确实给了我具有正确应用程序设置上下文的Django Shell(我已经通过将自定义变量TEST_VAR放在我的应用程序设置中,并且它在我的应用程序设置中正确地验证了它,并且它在我的应用程序设置中正确输出django shell通过 from django.conf import settings> settings.TEST_VAR)。

现在我更改myscript以运行我的自定义命令:

#!/bin/bash
./manage.py custom_command --settings=my.namespace.settings
PROMPT: Unknown command: 'custom_command'

我遵循以下说明来创建我的自定义django命令。

我的目录结构看起来如此:

my.namespace
    __init__.py          // & other django app files/dirs
    management           // directory
        __init__.py
        commands         // directory
            __init__.py
            custom_command.py

custom_command.py:

from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
    args = 'test'
    help = 'test help'
    def handle(self, *args, **kwargs):
        print 'test'

再次,运行myscript给了我unknown command。有什么想法吗?

您的 INSTALLED_APPS列表可能存在问题。检查您的settings.py文件以确认my.namespace包含在INSTALLED_APPS设置中。

最新更新