Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django manage.py settings default

Tags:

python

django

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?

like image 696
intargc Avatar asked Nov 29 '11 22:11

intargc


People also ask

Which is default database in settings file in Django?

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.

Where is settings py in Django?

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 .

What is settings py in Django?

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.


2 Answers

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.

like image 159
Ondrej Slinták Avatar answered Sep 22 '22 05:09

Ondrej Slinták


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.

like image 38
Chris Pratt Avatar answered Sep 21 '22 05:09

Chris Pratt