Django settings

  • A Django settings file contains all the configuration of your Django installation.
  • A settings file is just a Python module with module-level variables.
# setting example
ALLOWED_HOSTS = ['www.example.com']
DEBUG = False
DEFAULT_FROM_EMAIL = '[email protected]'

5.1 BASE_DIR

  • BASE_DIR points to top hierarchy of project
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# mysite/mysite/setting.py

5.2 DEBUG

  • In Development Error is very obvious to occur.
  • Django provides an inbuilt Debugger which makes the developer’s life very easy
# In Developement
DEBUG = True 

# In production, 
DEBUG = False

5.3 ALLOWED_HOSTS

  • ALLOWED_HOSTS is list having addresses of all domains which can run your Django Project.
  • It can not be an empty list
# 127.0.0.1 --> represents Your PC,
# *.heroku.com -->  represents heroku
# i.e. local PC and heroku can run my project
ALLOWED_HOSTS=[“127.0.0.1”, “*.heroku.com”]

5.4 INSTALLED_APPS

  • We mention all apps that will be used in our Django project.
INSTALLED_APPS = [
    # don't forget to quote it and also commas after every app
        'polls',
]

5.5 DATABASES

  • Django officially supports the following databases
    • PostgreSQL, MariaDB, MySQL, Oracle
    • SQLite <– By Default
# Default DB setting --> with SQLite
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase',
    }
}
  • PostgreSQL DB setup
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}
  • To use other Database change ENGINE
    • You can use a database backend that doesn’t ship with Django
    • By setting ENGINE to a fully-qualified path (i.e. mypackage.backends.whatever)
# ENGINE
'django.db.backends.postgresql'
'django.db.backends.mysql'
'django.db.backends.sqlite3'
'django.db.backends.oracle'

ALT

DEFAULT_DATABASE_CONFIG = dj_database_url.config(
        default='postgres://:ss', conn_max_age=120)
DEFAULT_DATABASE_CONFIG.update({
    'OPTIONS': {
        'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ,
    }
})
DATABASES = {
    'default': DEFAULT_DATABASE_CONFIG,
}

5.6 URL/Root Variables

  • STATIC_URL
    • It is the folder in each app, where Django will look for static files
  • MEDIA_URL
    • It is same as STATIC_URL but used for media files
  • STATICFILES_DIRS
    • It is the list of folders where Django will search for additional static files
    • Static assets that aren’t tied to a particular app
  • MEDIA_ROOT
    • It is the folder where files uploaded using FileField will go.
  • STATIC_ROOT
    • It is the folder where static files collected after using manage.py collectstatic
# Inside each app --> mysite/polls/static/
STATIC_URL = 'static/'
MEDIA_URL = '/media/'

# Project level
STATICFILES_DIRS = (
    # mysite/static_in_pro/frontend/
    os.path.join(BASE_DIR, "static_in_pro", "frontend"),

    # mysite/static/
    os.path.join(BASE_DIR, "static"),
)

5.7 Templates

  • Change Templates Dir
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        .... }]

5.8 Middleware

  • Middleware is a lightweight plugin (python class) that hooks into Django’s request/response life cycle
  • Middleware is used to perform a function in the application like security, session, csrf protection, authentication etc
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

order in MIDDLEWARE

  • The order in MIDDLEWARE matters because a middleware can depend on other middleware
    • For instance, AuthenticationMiddleware stores the authenticated user in the session;
    • Therefore, it must run after SessionMiddleware.
  • A Django installation doesn’t require any middleware — MIDDLEWARE can be empty,
    • if you’d like — but it’s strongly suggested that you at least use CommonMiddleware
  • The MIDDLEWARE list is like an onion
    • So each request passes through from top to bottom
    • And response is in reverse order (bottom to up).
  • CSRF middleware
    • csrf_protect()
    • csrf_token tag
    • csrf_exempt()

https://vegibit.com/how-to-create-and-use-custom-django-middleware/

setting.py

LOGIN_REDIRECT_URL = ‘home’ LOGIN_URL = ‘login’

Reference