Django local_settings. 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 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 be learning the following for optimizing your Django Project โ€“

๐Ÿ”ด Best Practice: Configure and O...
๐Ÿ”ด Best Practice: Configure and Optimize File in Django 2020

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 will look like this โ€“


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 โ€“


     # add apps which you install using pip

     # add local apps which you create using startapp

 # Application definition

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 and import it in file.

Remove the database code from file and add it to

# Database

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

And import that file in file at the end โ€“

# ...
# Loading from file
    from django_shop.local_settings import *
except ImportError:

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 file. Now that we have created a template folder, we need to tell our Django project about that template path. So in your file add the path like this-

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR + '/templates/',], # <- here
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [

Add Static and Media Paths

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

Add the following to your file โ€“

# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'
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 file.

Also you can see full and files on GitHub.