Splitting settings

Article Content


It's possible to split django settings into parts - this can be very usefull, for example with development process where you must have different settings for sproduction server and development server.

  1. Create in django root folder with name 'settings'
  2. Rename 'settings.py' to 'base.py'
  3. Move 'base.py' into 'settings' folder
  4. Create new settings files, below examples for 'dev.py' (settings for development server) and 'production.py' (settings for production server):

#dev.py

# IMPORTANT - MUST BE ON THE TOP OF THE FILE
from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Print email in console instead send real email
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# Allow all hosts
ALLOWED_HOSTS = ['*']

try:
    from .local import *
except ImportError:
    pass
    

#production.py

# IMPORTANT - MUST BE ON THE TOP OF THE FILE
from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

# Don't worry it's not actually real site secret key :) 
SECRET_KEY = 'nr329rj239e193i012-3ke102mje19nh21983j103j102'

# Allow all hosts
ALLOWED_HOSTS = ['wiki.grind-dev.com']

try:
    from .local import *
except ImportError:
    pass
    

   5. Edit manage.py to use 'dev.py' settings


#!/usr/bin/env python
from __future__ import absolute_import, unicode_literals

import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "grindwiki.settings.dev")         ##### Replace grindwiki with your project name

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)
    

   6. Edit wsgi.py file for use 'production.py' settings


import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "grindwiki.settings.production")   ##### Replace grindwiki with your project name

application = get_wsgi_application()
    

SubArticles


About

Added by dominik

Date: Aug. 8, 2018, 12:14 p.m.

Comments