Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Local Settings

I'm trying to use local_setting in Django 1.2, but it's not working for me. At the moment I'm just adding local_settings.py to my project.

settings.py

DATABASES = {     'default': {         'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.         'NAME': 'banco1',                      # Or path to database file if using sqlite3.         'USER': 'root',                      # Not used with sqlite3.         'PASSWORD': '123',                  # Not used with sqlite3.         'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.         'PORT': '',                      # Set to empty string for default. Not used with sqlite3.     } } 

local_settings.py

DATABASES = {     'default': {         'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.         'NAME': 'banco2',                      # Or path to database file if using sqlite3.         'USER': 'root',                      # Not used with sqlite3.         'PASSWORD': '123',                  # Not used with sqlite3.         'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.         'PORT': '',                      # Set to empty string for default. Not used with sqlite3.     } } 

The problem is that local_settings.py doesn't override settings.py. What is wrong?

like image 697
Michel Andrade Avatar asked Feb 05 '11 21:02

Michel Andrade


People also ask

How does Django manage local vs production settings?

If you point it to app/settings_local.py on your local server and app/settings_production.py on your production server then life becomes easy. Just edit the appropriate settings file and restart the server (Django development server will restart automatically).

Where is Django settings file?

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 .


1 Answers

You can't just add local_settings.py, you have to explicity import it.

At the very end of your settings.py, add this:

try:     from local_settings import * except ImportError:     pass 

The try/except block is there so that Python just ignores the case when you haven't actually defined a local_settings file.

like image 75
Daniel Roseman Avatar answered Oct 10 '22 17:10

Daniel Roseman