I have a settings.py
file and a dev_settings.py
file that I use to override some values for dev purposes. Everytime I run the ./manage.py
command, I have to specify --settings=whatever.local_settings
. This becomes very tedious to do every time and I am trying to find a way to force manage.py to load my dev_settings.py file every by default so that I don't have to type that long argument every time I want to run a command.
I have tried setting DJANGO_SETTINGS_MODULE
, however, it appears that manage.py overrides this option.
Is it possible to make this happen or am I doomed to always specify that argument?
By default, the configuration uses SQLite. If you're new to databases, or you're just interested in trying Django, this is the easiest choice. SQLite is included in Python, so you won't need to install anything else to support your database.
A Django settings file doesn't have to define any settings if it doesn't need to. Each setting has a sensible default value. These defaults live in the module django/conf/global_settings.py .
settings.py is a core file in Django projects. It holds all the configuration values that your web app needs to work; database settings, logging configuration, where to find static files, API keys if you work with external APIs, and a bunch of other stuff.
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:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With