Templates help you create the HTML pages your website shows. Configuring templates tells Django where to find these HTML files.
Template configuration and directories in Django
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'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',
],
},
},
]DIRS is a list of folders where Django looks for templates outside apps.
APP_DIRS=True tells Django to look inside each app's templates folder automatically.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], # No extra folders
'APP_DIRS': True,
'OPTIONS': {},
},
]custom_templates folder, not inside apps.TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'custom_templates'],
'APP_DIRS': False,
'OPTIONS': {},
},
]TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates', BASE_DIR / 'extra_templates'],
'APP_DIRS': True,
'OPTIONS': {},
},
]This example shows how to set up the template folders and print them out. It uses BASE_DIR to find the main project folder and adds a templates folder there. It also enables looking inside app folders.
from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR / 'templates'], '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', ], }, }, ] print("Template directories:") for d in TEMPLATES[0]['DIRS']: print(d) print("App directories enabled:", TEMPLATES[0]['APP_DIRS'])
Always create a templates folder in your project root or inside each app for Django to find your HTML files.
If APP_DIRS is True, Django looks inside each app's templates folder automatically.
You can add many folders in DIRS if you want to organize templates in different places.
Templates are HTML files that Django uses to show pages.
Configure TEMPLATES in settings.py to tell Django where to find these files.
Use DIRS for custom folders and APP_DIRS=True to find templates inside apps.