📖 Scenario: You are building a simple Django project that will use HTML templates to render web pages. To do this, you need to set up the template directories and configure Django to find your templates correctly.
🎯 Goal: Set up the Django template directories and configure the TEMPLATES setting in settings.py so Django can load templates from a custom folder called templates inside your project directory.
📋 What You'll Learn
Create a templates folder inside the Django project directory
Configure the TEMPLATES setting in settings.py to include the templates directory
Ensure the APP_DIRS option is set to True
Use the correct DIRS list format with the project base directory joined with templates
💡 Why This Matters
🌍 Real World
Setting up templates correctly is essential for any Django web project to render HTML pages dynamically.
💼 Career
Knowing how to configure templates and directories is a fundamental skill for Django developers working on web applications.
Progress0 / 4 steps
1
Create the templates directory
Create a folder named templates inside your Django project directory (the same directory as settings.py).
Django
Hint
Use your file explorer or terminal to create a folder named templates inside the project folder where settings.py is located.
2
Import os and define BASE_DIR in settings.py
In settings.py, import the os module and define BASE_DIR as os.path.dirname(os.path.dirname(os.path.abspath(__file__))) to get the project base directory.
Django
Hint
Use import os at the top of settings.py. Then define BASE_DIR using os.path functions as shown.
3
Configure the TEMPLATES setting with DIRS and APP_DIRS
In settings.py, update the TEMPLATES setting so that the DIRS list contains os.path.join(BASE_DIR, 'templates') and APP_DIRS is set to True.
Django
Hint
Set 'DIRS' to a list with os.path.join(BASE_DIR, 'templates'). Set 'APP_DIRS' to True.
4
Verify template loading configuration
Ensure the TEMPLATES setting in settings.py is correctly configured with BASE_DIR, DIRS containing os.path.join(BASE_DIR, 'templates'), and APP_DIRS set to True to complete the template configuration.
Django
Hint
Double-check that BASE_DIR, DIRS, and APP_DIRS are set exactly as required.
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
Step 1: Understand the role of TEMPLATES['DIRS']
This setting tells Django where to find templates that are not inside app folders.
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 A
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
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.
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.
Final Answer:
Set APP_DIRS=True in the TEMPLATES setting -> Option C
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
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
Step 1: Enable automatic app template loading
Setting APP_DIRS=True allows Django to find templates inside each app's templates folder.
Step 2: Add shared templates folder to DIRS
Adding the shared folder path to DIRS lets Django find templates used across the whole project.
Final Answer:
Set APP_DIRS=True and add the shared templates folder path to DIRS -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.