1

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?

4
  • Shouldn't you indent handle() into the class? Also have you tried running the command "by hand" first? Commented Jun 21, 2016 at 21:20
  • @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. Commented Jun 22, 2016 at 12:51
  • Could you please provide the actual directory structure (with the real namespace and command names) rather than this example? Commented Jun 22, 2016 at 14:21
  • 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? Commented Jun 23, 2016 at 13:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.