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.
Practice
TEMPLATES['DIRS'] setting in Django?Solution
Step 1: Understand the role of
This setting tells Django where to find templates that are not inside app folders.TEMPLATES['DIRS']Step 2: Differentiate from other settings
Other options like installed apps or debugging are unrelated to template directories.Final Answer:
To specify additional directories where Django looks for templates outside apps -> Option AQuick Check:
Template directories =TEMPLATES['DIRS'][OK]
- Confusing DIRS with installed apps list
- Thinking DIRS enables debugging
- Assuming DIRS defines the template engine
Solution
Step 1: Identify the setting for automatic app template discovery
SettingAPP_DIRS=Truetells Django to look inside each app's templates folder automatically.Step 2: Understand why other options are incorrect
SettingAPP_DIRS=Falsedisables this feature; manually adding paths is not automatic; installed apps list alone doesn't find templates.Final Answer:
SetAPP_DIRS=Truein the TEMPLATES setting -> Option CQuick Check:
Automatic app templates =APP_DIRS=True[OK]
- Setting APP_DIRS to False expecting auto-discovery
- Adding app templates manually instead of using APP_DIRS
- Confusing INSTALLED_APPS with template directories
settings.py snippet: TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {},
}]Where will Django look for templates when rendering?
Solution
Step 1: Analyze the
The 'DIRS' list includes 'templates', so Django looks there first.DIRSsettingStep 2: Analyze the
SinceAPP_DIRSsettingAPP_DIRS=True, Django also searches inside each app's 'templates' folder.Final Answer:
Both in the 'templates' folder and inside each app's 'templates' folder -> Option DQuick Check:
DIRS + APP_DIRS = both locations searched [OK]
- Thinking DIRS disables app templates
- Assuming APP_DIRS=True ignores DIRS
- Believing DIRS must be empty for app templates
APP_DIRS=False but your templates inside app folders are not found. What is the best fix?Solution
Step 1: Understand the effect of
WithAPP_DIRS=FalseAPP_DIRS=False, Django does not look inside app template folders automatically.Step 2: Choose the fix to enable automatic app template discovery
SettingAPP_DIRS=Truetells Django to find templates inside app folders without manual paths.Final Answer:
SetAPP_DIRS=Trueto enable automatic app template loading -> Option BQuick Check:
APP_DIRS=True fixes app template loading [OK]
- Forgetting to set APP_DIRS=True
- Only restarting server without config change
- Removing DIRS thinking it fixes app templates
TEMPLATES to achieve this?Solution
Step 1: Enable automatic app template loading
SettingAPP_DIRS=Trueallows Django to find templates inside each app's templates folder.Step 2: Add shared templates folder to
Adding the shared folder path toDIRSDIRSlets Django find templates used across the whole project.Final Answer:
SetAPP_DIRS=Trueand add the shared templates folder path toDIRS-> Option AQuick Check:
APP_DIRS + DIRS for shared + app templates [OK]
- Disabling APP_DIRS and expecting app templates found
- Duplicating shared templates inside apps
- Leaving DIRS empty and missing shared templates
Ready to prove it?
Pass the test and this pattern moves unknown
