Saturday, December 28, 2024
Google search engine
HomeLanguagesDjango settings file – step by step Explanation

Django settings file – step by step Explanation

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 manage.py startapp polls

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

mysite           <-- BASE_DIR      
    --> mysite                 
            -> __init__.py
            -> asgi.py
            -> settings.py    <-- settings.py file 
            -> urls.py
            -> wsgi.py
    --> manage.py
    --> polls

Insights about the settings.py file

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

BASE_DIR

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__)))

DEBUG

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

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 127.0.0.1 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=[“127.0.0.1”, “*.heroku.com”]. “127.0.0.1” represents Your PC, “*.heroku.com” represents this application can be run on heroku also. 

INSTALLED_APPS

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: 

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

DATABASES

Django officially supports the following databases: 

  • PostgreSQL
  • MariaDB
  • MySQL
  • Oracle
  • SQLite <– By Default
    DATABASES = {
        '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 

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': YOUR_DB_NAME,
            'USER': USERNAME,
            'PASSWORD': PASSWORD_FOR_DB,
            '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:

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

MEDIA_URL= '/media/'

STATIC_URL:

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:

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:

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.

RELATED ARTICLES

Most Popular

Recent Comments