Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Django Custom Management Command - Path Issues

Tags:

python

django

I don't understand why this doesn't find the command:

khandelwal@simba:/opt/code/cdcschool$ python schoolcommand/manage.py createcampaign
Unknown command: 'createcampaign'
Type 'manage.py help' for usage.

When this works fine:

khandelwal@simba:/opt/code/cdcschool$ cd schoolcommand/
khandelwal@simba:/opt/code/cdcschool/schoolcommand$ python manage.py createcampaign
Error: Provide: <type start_date end_date>

This is where my command is located:

$ ls schoolcommand/management/
commands  __init__.py  __init__.pyc

$ ls schoolcommand/management/commands/
campaignmanager.py   createcampaign.py   __init__.py
campaignmanager.pyc  createcampaign.pyc  __init__.pyc

How do I fix it so that I can do:

khandelwal@simba:/opt/code/cdcschool$ python schoolcommand/manage.py createcampaign

Here are the values of my PYTHONPATH, DJANGO_SETTINGS_MODULE and the one place my settings.py is located.

khandelwal@simba:/opt/code/cdcschool$ ls
Procfile  README  requirements.txt  schoolcommand
khandelwal@simba:/opt/code/cdcschool$ echo $PYTHONPATH

khandelwal@simba:/opt/code/cdcschool$ echo $DJANGO_SETTINGS_MODULE

khandelwal@simba:/opt/code/cdcschool$ find . -name settings.py
./schoolcommand/settings.py
khandelwal@simba:/opt/code/cdcschool$
like image 411
Khandelwal Avatar asked Sep 23 '26 07:09

Khandelwal


1 Answers

When you use Django, there are two important rules.

First.

You have a settings.py file which must be used by the web server and all the manage.py commands. All of them.

The default place to look for the settings.py file is the current working directory. You can change this with the PYTHONPATH and the DJANGO_SETTINGS_MODULE environment variable.

The manage.py is created for you in the same directory as the settings.py.

You can use django-admin.py --settings=some.module if you don't want to use manage.py.

Second.

The manage.py commands do not have any "path" to them. They're all just one-word commands, no matter where they happen to live in your application tree.

You never do this: python schoolcommand/manage.py createcampaign unless (somehow) your settings.py is not in the same directory as your manage.py.

You normally do this:

cd /path/to/your/settings
python manage.py createcampaign

If your settings is in code/schoolcommand that means that your web site and all your commands will operate in that directory.

like image 110
S.Lott Avatar answered Sep 25 '26 19:09

S.Lott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!