Django Best Practice: Configuring Settings File

Django Best Practice: Configuring Settings File

Developing Django Application needs configurations and settings. Managing these configurations is a must for projects. We need to configure projects for different environments like testing, staging, production, and development. It is important and mandatory for the developers, to manage and separate default and new configurations.

Django does not provide a way to have separate configurations. All project-related configurations are put in settings.py file. There are several methods to meet the above requirements, however, my favorite one is having different local settings module and everything distinct.

You will the following for optimizing your Django Project –

Separate Django Application definition – INSTALLED_APPS

For Enabling Apps, we need to add Apps in INSTALLED_APPS settings. Apps can be of different types like a library, third-party apps, project apps, and default apps. Everything adding in INSTALLED_APPS will look confused.

By Default, INSTALLED_APPS in settings.py will look like this –

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

To solved the above problem, I have found a good technique. We will separate the List for default apps, third-party apps, and local apps.

Replace your INSTALLED_APPS with the following –

DEFAULT_APPS = [
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
 ]

 THIRD_PARTY_APPS =[
     # add apps which you install using pip
 ]

 LOCAL_APPS =[
     # add local apps which you create using startapp
 ]

 # Application definition
 INSTALLED_APPS = DEFAULT_APPS + THIRD_PARTY_APPS + LOCAL_APPS

Separate Django Database Local Settings

Projects are first created and develop in development mode, then it shifted to production mode. The projects are shifted to different environments and machines. Every environment has different credentials and settings for the database, so we need to change it every time.

To avoid code break and separate local database values, we will create a file name local_settings.py and import it in settings.py file.

Remove the database code from settings.py file and add it to local_settings.py

# local_settings.py
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django_shop', 
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '80',
    }
}

And import that local_settings.py file in settings.py file at the end –

# settings.py
# ...
# Loading from local_settings.py file
try:
    from django_shop.local_settings import *
except ImportError:
    pass

Change Django Template Paths Directory

Instead of keeping all your template under the apps folder, I like to create a separate folder and keep all apps template in that.

Create a template folder named templates at the level of the manage.py file. Now that we have created a template folder, we need to tell our Django project about that template path. So in your settings.py file add the path like this-

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR + '/templates/',], # <- here
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Add Static and Media Paths

For making our static and media files to serve, we need to add the path to our settings.py file. Make sure django.contrib.staticfiles is added to INSTALLED_APPS, by default it there.

Add the following to your settings.py file –

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = (     
os.path.join(BASE_DIR, 'static'), 
) 
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Note – Our media and static folder are at the level of manage.py file.

Also you can see full settings.py and local_settings.py files on GitHub.

Share