Skip to content
Related Articles

Related Articles

Django settings file – step by step Explanation

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 01 Jun, 2021

Once we create the Django project, it comes with predefined Directory structure having the following files with each file having their own uses. 

Lets take an example 

// Create a Django Project "mysite" 
django-admin startproject mysite

cd /pathTo/mysite
// Create a Django app "polls" inside project "mysite"
python startapp polls

After these commands on Terminal/CMD Directory structure of project “mysite” will be: 

mysite           <-- BASE_DIR      
    --> mysite                 
            ->    <-- file 
    --> polls


Insights about file

A Django settings file contains all the configuration of your Django Project. In this article the important points of file of Django will be discussed. 
A settings file is just a Python module with module-level variables. 


BASE_DIR points to top hierarchy of project i.e. mysite, whatever paths we define in project are all relative to BASE_DIR. To use BASE_DIR we will have to use os module provided by python. 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))



In Development Error is very obvious to occur. There is no fun in writing a program where we do not face any error. but sometimes tackling errors is very hectic. Django provides an inbuild Debugger which makes the developer’s life very easy. We can use it by doing: 

DEBUG = True  // It is Default value and is preferred in only Development Phase.

In production, DEBUG = False is preferred. 



ALLOWED_HOSTS is list having addresses of all domains which can run your Django Project. 

When DEBUG set to True 
ALLOWED_HOSTS can be an empty list i.e. ALLOWED_HOSTS=[ ] because by Default it is or localhost 
When DEBUG set to False 
ALLOWED_HOSTS can not be an empty list. We have to give hosts name in list. i.e. ALLOWED_HOSTS=[“”, “*”]. “” represents Your PC, “*” represents this application can be run on heroku also. 



In this section, we mention all apps that will be used in our Django project. Previously we made an app polls we have to tell Django its existence To do so have to put into INSTALLED_APPS: 

        // Some preloaded apps by Django,
        'polls', // don't forget to quote it and also commas after every app



Django officially supports the following databases: 

  • PostgreSQL
  • MariaDB
  • MySQL
  • Oracle
  • SQLite <– By Default


        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

There are also a number of database backends provided by third parties
Here is Example of using PostgreSQL 

        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': YOUR_DB_NAME,
            'USER': USERNAME,
            'HOST': 'localhost'  // in Development.


Note: Before using PostgreSQL we have to install psycopg2 using 
pip install psycopg2



URL Variables

URL variables are relative to BASE_DIR. These variables are used to store files either media or static. 
Note: Make static and media folders in Parent Directory. 


MEDIA_URL is relative path to BASE_DIR. This variable is use to store the media files. 

MEDIA_URL= '/media/'



STATIC_URL is relative path to BASE_DIR. This variable is use to store the static files. 

STATIC_URL = '/static/'


ROOT Variables

ROOT variables are absolute paths. These variables are used to retrieve files either media or static. 



MEDIA_ROOT is absolute path. This variable is use to retrieve the media files. 

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



STATIC_ROOT is absolute path. This variable is use to retrieve the static files. 

STATIC_ROOT= os.path.join(BASE_DIR, 'static')


Note: All variable names in Django settings are in CAPITAL.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!