Skip to content
Django
11. Django Files Cheatsheet
Settings

Settings

  • It manages database connection, template url connection, static url connection.
  • You don't need to write this code it is pre builded.
settings.py
"""
Django settings for wewrite project.
 
Generated by 'django-admin startproject' using Django 5.0.
 
For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/
 
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""
 
from pathlib import Path
import os
 
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
 
 
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
 
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-2k3wi04ow8z2omd!q&h#nh9(xd&rs7vxph95#9e9x)icu9r9l+'
 
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
 
# stores localhost, change port server.
ALLOWED_HOSTS = []
 
 
# Application definition
 
# INSTALLED_APPS - Default table migrate, store here.
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # ============== Install Your Apps Here
    #### App Config - On New App Creation
    'pages.apps.PagesConfig',   # python manage.py startapp pages
    #### Crispy Forms - Crispy forms are used to style (Bootstrap) the djagno built-in form that is created by `UserCreationForm`
    'crispy_forms',     # pip install django-crispy-forms
    'crispy_bootstrap5',  # pip install crispy-bootstrap5
    #### Django REST Framework
    
]
 
 
# Add in the last line of the file - For Crispy-Bootstrap5
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = "bootstrap5"
 
 
# Middleware Settings - Restriction to not direct open admin, sessions.
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',
]
 
ROOT_URLCONF = 'wewrite.urls'
 
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'], # Add your templates file here
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                # Add context_processors for global variables access across templates
                'config.template_global_variables.global_variables',    # global_variables function from config/template_global_variables.py
            ],
        },
    },
]
 
WSGI_APPLICATION = 'wewrite.wsgi.application'
 
 
# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
 
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
 
 
# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
# Login/ Password Admin Validation
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]
 
 
# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
 
 
# Timezone Settings
TIME_ZONE =  'Asia/Kolkata'  # For India
# TIME_ZONE = 'UTC'
 
# Internationalization Settings
USE_I18N = True
USE_TZ = True
 
 
# Static files Settings - CSS, JavaScript, Images
# https://docs.djangoproject.com/en/5.0/howto/static-files/
STATIC_URL = 'static/'
STATICFILES_DIRS = [BASE_DIR / 'static',] # static storage
# Static Root Settings
STATIC_ROOT = BASE_DIR / 'staticfiles'  # python manage.py collectstatic
 
# Media Files Settings
# To use media files you have to install - pip install Pillow
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / "media"
 
 
# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
 
 
# Redirect Settings
# Login Redirect URL
LOGIN_REDIRECT_URL = 'blog-home'    # It redirects to home page when user is logged in
LOGIN_URL = 'login'                 # Django use default url "accounts/login" for redirecting when without login.
# We are changing it to "login" so that it will redirect to our login page.
# Logout URL for logout view when user is logged in
LOGOUT_URL = 'logout'
 
 
# Email Password Reset Settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS =  True
EMAIL_HOST_USER = os.environ.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')