Bird
Raised Fist0
Djangoframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the purpose of the TEMPLATES['DIRS'] setting in Django?
easy
A. To specify additional directories where Django looks for templates outside apps
B. To list all installed apps in the project
C. To enable debugging mode for templates
D. To define the default template engine

Solution

  1. Step 1: Understand the role of TEMPLATES['DIRS']

    This setting tells Django where to find templates that are not inside app folders.
  2. Step 2: Differentiate from other settings

    Other options like installed apps or debugging are unrelated to template directories.
  3. Final Answer:

    To specify additional directories where Django looks for templates outside apps -> Option A
  4. Quick Check:

    Template directories = TEMPLATES['DIRS'] [OK]
Hint: Remember: DIRS means extra folders for templates [OK]
Common Mistakes:
  • Confusing DIRS with installed apps list
  • Thinking DIRS enables debugging
  • Assuming DIRS defines the template engine
2. Which of the following is the correct way to enable Django to automatically find templates inside app folders?
easy
A. Set APP_DIRS=False in the TEMPLATES setting
B. Add app template paths manually to TEMPLATES['DIRS']
C. Set APP_DIRS=True in the TEMPLATES setting
D. Include app names in INSTALLED_APPS only

Solution

  1. Step 1: Identify the setting for automatic app template discovery

    Setting APP_DIRS=True tells Django to look inside each app's templates folder automatically.
  2. Step 2: Understand why other options are incorrect

    Setting APP_DIRS=False disables this feature; manually adding paths is not automatic; installed apps list alone doesn't find templates.
  3. Final Answer:

    Set APP_DIRS=True in the TEMPLATES setting -> Option C
  4. Quick Check:

    Automatic app templates = APP_DIRS=True [OK]
Hint: APP_DIRS=True means auto-find app templates [OK]
Common Mistakes:
  • Setting APP_DIRS to False expecting auto-discovery
  • Adding app templates manually instead of using APP_DIRS
  • Confusing INSTALLED_APPS with template directories
3. Given this settings.py snippet:
 TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['templates'],
    'APP_DIRS': True,
    'OPTIONS': {},
}]

Where will Django look for templates when rendering?
medium
A. Only in the 'templates' folder at project root
B. Only inside each app's 'templates' folder
C. Nowhere, because 'DIRS' must be empty to work
D. Both in the 'templates' folder and inside each app's 'templates' folder

Solution

  1. Step 1: Analyze the DIRS setting

    The 'DIRS' list includes 'templates', so Django looks there first.
  2. Step 2: Analyze the APP_DIRS setting

    Since APP_DIRS=True, Django also searches inside each app's 'templates' folder.
  3. Final Answer:

    Both in the 'templates' folder and inside each app's 'templates' folder -> Option D
  4. Quick Check:

    DIRS + APP_DIRS = both locations searched [OK]
Hint: DIRS + APP_DIRS=True means search both places [OK]
Common Mistakes:
  • Thinking DIRS disables app templates
  • Assuming APP_DIRS=True ignores DIRS
  • Believing DIRS must be empty for app templates
4. You set APP_DIRS=False but your templates inside app folders are not found. What is the best fix?
medium
A. Add app template paths manually to TEMPLATES['DIRS']
B. Set APP_DIRS=True to enable automatic app template loading
C. Remove the 'DIRS' setting completely
D. Restart the Django server without changing settings

Solution

  1. Step 1: Understand the effect of APP_DIRS=False

    With APP_DIRS=False, Django does not look inside app template folders automatically.
  2. Step 2: Choose the fix to enable automatic app template discovery

    Setting APP_DIRS=True tells Django to find templates inside app folders without manual paths.
  3. Final Answer:

    Set APP_DIRS=True to enable automatic app template loading -> Option B
  4. Quick Check:

    APP_DIRS=True fixes app template loading [OK]
Hint: APP_DIRS=True enables automatic app template search [OK]
Common Mistakes:
  • Forgetting to set APP_DIRS=True
  • Only restarting server without config change
  • Removing DIRS thinking it fixes app templates
5. You want to organize templates so that some are shared across the project and others are app-specific. How should you configure TEMPLATES to achieve this?
hard
A. Set APP_DIRS=True and add the shared templates folder path to DIRS
B. Set APP_DIRS=False and put all templates in one shared folder listed in DIRS
C. Put all templates inside app folders and leave DIRS empty
D. Disable APP_DIRS and duplicate shared templates inside each app

Solution

  1. Step 1: Enable automatic app template loading

    Setting APP_DIRS=True allows Django to find templates inside each app's templates folder.
  2. Step 2: Add shared templates folder to DIRS

    Adding the shared folder path to DIRS lets Django find templates used across the whole project.
  3. Final Answer:

    Set APP_DIRS=True and add the shared templates folder path to DIRS -> Option A
  4. Quick Check:

    APP_DIRS + DIRS for shared + app templates [OK]
Hint: Use APP_DIRS=True plus shared folder in DIRS [OK]
Common Mistakes:
  • Disabling APP_DIRS and expecting app templates found
  • Duplicating shared templates inside apps
  • Leaving DIRS empty and missing shared templates
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard