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:
-
set
DJANGO_SETTINGS_MODULE
and usedjango-admin.py
to run all commands instead ofmanage.py
. This is even better if you use vitualenv. -
copy
manage.py
and name itlocal.py
(that’s the name in my case) and rename allsettings
mentions todev_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.
- Django Rest Framework Nested Serializers
- Django-templates: Why doesn't {% if "string"|length > 10 %} work at all?
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.
- In Django admin, how can I hide Save and Continue and Save and Add Another buttons on a model admin?
- Django-templates: Why doesn't {% if "string"|length > 10 %} work at all?
- How can I programmatically add content to a Wagtail StreamField?
- Find django/contrib/admin/templates
- How to get pretty output from rest_framework serializer
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)
- Pass initial value to a modelform in django
- Django AttributeError: 'DatabaseOperations' object has no attribute 'select'
- How to print pretty JSON on a html page from a django template?
- Django template variable value to string literal comparison fails