0
0
Djangoframework~30 mins

Template configuration and directories in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Template configuration and directories
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Double-check that BASE_DIR, DIRS, and APP_DIRS are set exactly as required.