Open In App

Django settings file – step by step Explanation

Last Updated : 28 Apr, 2023
Like Article

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

Let’s 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 the Terminal/CMD Directory structure of project “mysite” will be: 

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

Insights about the file

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


BASE_DIR points to the top hierarchy of the project i.e. mysite, whatever paths we define in the 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 inbuilt 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 a 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 an 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 the relative path to BASE_DIR. This variable is used to store the media files.  

MEDIA_URL= '/media/'


STATIC_URL is the relative path to BASE_DIR. This variable is used 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 an absolute path. This variable is used to retrieve the media files. 

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


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

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

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

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads