[Fixed]-Django manage.py settings default

17👍

manage.py sets path to settings for you, that’s why it’s ignoring DJANGO_SETTINGS_MODULE (it’s basically just script that wraps around django-admin.py).

There are 2 easy ways to fix your problem:

  1. set DJANGO_SETTINGS_MODULE and use django-admin.py to run all commands instead of manage.py. This is even better if you use vitualenv.

  2. copy manage.py and name it local.py (that’s the name in my case) and rename all settings mentions to dev_settings.

For example:

#!/usr/bin/env python
from django.core.management import execute_manager
import imp

try:
    import settings_local
except ImportError:
    import sys
    sys.stderr.write("Error: Can't find the file 'settings_local.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
    sys.exit(1)

if __name__ == "__main__":
    execute_manager(settings_local)

You can run all commands by ./local.py now.

3👍

The way this is typically done is you have settings.py with all settings that are common between environments (things like INSTALLED_APPS, etc.). Then, you have something like settings_local.py, that defines settings particular to the environment in context. You then import settings_local.py in settings.py.

# settings.py

from settings_local import *

settings.py gets added to your source code repository, but settings_local.py does not. (However, you would normally add something like settings_local.py.example to the repo.)

When you first move your app over to production, for example, you pull down the code base from your repo. You then copy settings_local.py.example to settings_local.py and make any necessary environment specific changes.

You then have separate settings_local.py files in each environment, and it all just works.

2👍

You can make a bash alias by adding these lines to your .bash_profile file:

mymanage()
{
python manage.py $1 --settings=settings_debug
}
alias mng=mymanage

Then when you run this command:

mng runserver

settings_debug.py file will be used for settings.

1👍

You can use django-admin.py with that environment variable. Commands are interchangeable, only django-admin.py doesn’t override the variable you’re trying to use.

1👍

If a settings file is common to all installation, you can just import it e.g.

from settings_local import *

but usually settings_local are changed and tweaked per installation and as my installation script directly copy files to target sites (without worrying what is local what is not), which mean settings_local may get overwritten, to avoid that I just keep settings_local in parent folder of the installation target and manually import it in settings.py e.g.

local_settings_file = os.path.join(prevFolder, "settings_local.py")
if os.path.exists(local_settings_file):
    execfile(local_settings_file)

Leave a comment