1
votes

Using django 1.5

I want to use a shell script to run a custom django manage.py command while I'm passing in the settings (i.e. not using the settings from the same directory).

As an example of passing in my settings, if I run my django development server I'd do so like this:

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

In the same directory as my manage.py file, I have my shell script: myscript

Current contents of myscript:

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

From the directory of myscript, running ./myscript does indeed give me the django shell with the context of the correct app settings (I've verified that by putting a custom variable, TEST_VAR, in my app settings and it outputs correctly in the django shell via from django.conf import settings > settings.TEST_VAR).

Now I change myscript to run my custom command:

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

I've followed these instructions to create my custom django command.

My directory structure looks as such:

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'

Again, running myscript gives me unknown command. Any ideas?

1
Shouldn't you indent handle() into the class? Also have you tried running the command "by hand" first? - Vedran
@Vedran I've tried both ways. What do you mean run the command 'by hand'? Running the python file gives me errors about not having django environment variables. - smilebomb
Could you please provide the actual directory structure (with the real namespace and command names) rather than this example? - 2ps
gives me errors about not having django environment variables usually when I get errors like those it's because I'm not sourced into my virtual environment. Are you using virtualenv? - Vedran

1 Answers

1
votes

The problem is likely with you INSTALLED_APPS list. Check your settings.py file to confirm that my.namespace is included in the INSTALLED_APPS settings.