0
0
Djangoframework~10 mins

Template configuration and directories in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Template configuration and directories
Start Django Project
Define TEMPLATES in settings.py
Set 'DIRS' to template folders
Place HTML files in template directories
Django loads templates from DIRS and app templates
Render template in views
Browser shows rendered HTML
This flow shows how Django uses the TEMPLATES setting to find and load HTML template files from configured directories, then renders them in views to display in the browser.
Execution Sample
Django
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
    },
]
This code configures Django to look for templates in the 'templates' folder at the project base and inside each app's templates folder.
Execution Table
StepActionSettings StateTemplate Lookup PathResult
1Start Django projectTEMPLATES not setNo template directoriesNo templates loaded
2Define TEMPLATES in settings.pyTEMPLATES set with DIRS=[BASE_DIR / 'templates'], APP_DIRS=TrueProject 'templates' folder and app templatesReady to find templates
3Place 'home.html' in 'templates' folderNo changetemplates/home.htmlTemplate file exists
4Call render(request, 'home.html') in viewNo changeSearch order: DIRS then app templatesFinds 'home.html' in 'templates' folder
5Render template with contextNo changeN/AHTML generated and sent to browser
6Browser receives responseNo changeN/AUser sees rendered page
7If template missingNo changeNo file foundTemplateDoesNotExist error
💡 Execution stops after template is rendered or error occurs if template not found.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
TEMPLATESundefinedConfigured with DIRS and APP_DIRSNo changeNo changeNo changeNo change
Template Pathnone['templates'] and app templates'templates/home.html' existsTemplate found in 'templates'Rendered HTML outputRendered HTML output
Key Moments - 3 Insights
Why does Django look in both the DIRS folders and app templates?
Because 'DIRS' lists project-level template folders, and 'APP_DIRS=True' tells Django to also look inside each app's 'templates' folder. This is shown in execution_table step 4.
What happens if the template file is not found in any directory?
Django raises a TemplateDoesNotExist error, as shown in execution_table step 7, stopping rendering.
How does Django know where the 'templates' folder is in DIRS?
In settings.py, 'DIRS' is set with an absolute or BASE_DIR path to the 'templates' folder, so Django can find it. This is shown in execution_sample code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Django find the template file?
AStep 3
BStep 5
CStep 4
DStep 7
💡 Hint
Check the 'Result' column in execution_table rows for when the template is found.
According to variable_tracker, what is the state of TEMPLATES after step 2?
AConfigured with DIRS and APP_DIRS
BRendered HTML output
CUndefined
DTemplateDoesNotExist error
💡 Hint
Look at the 'TEMPLATES' row and 'After Step 2' column in variable_tracker.
If APP_DIRS was set to False, how would the template lookup change?
ADjango would look in both DIRS and app templates
BDjango would only look in DIRS folders
CDjango would not look in any folder
DDjango would only look in app templates
💡 Hint
Refer to key_moments about APP_DIRS behavior and execution_table step 4.
Concept Snapshot
Django templates are HTML files rendered with data.
Configure template folders in settings.py under TEMPLATES with 'DIRS' and 'APP_DIRS'.
'DIRS' lists project-level folders; 'APP_DIRS=True' includes app templates.
Place HTML files in these folders.
Use render() in views to load and display templates.
If template missing, Django raises an error.
Full Transcript
This visual execution trace shows how Django finds and uses templates. First, you set the TEMPLATES setting in settings.py, specifying directories in 'DIRS' and enabling 'APP_DIRS' to include app templates. Then, you place your HTML files in these folders. When a view calls render() with a template name, Django searches these directories in order. If it finds the template, it renders it with any data and sends the HTML to the browser. If not found, it raises an error. Tracking variables like TEMPLATES and template paths helps understand this process step-by-step.