Bird
Raised Fist0
Djangoframework~10 mins

Template configuration and directories in Django - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add the templates directory path in Django settings.

Django
TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [[1]],
    'APP_DIRS': True,
}]
Drag options to blanks, or click blank then click option'
A'templates/'
B'media/'
C'static/'
D'scripts/'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static/' instead of 'templates/'
Leaving DIRS empty when you have custom template folders
2fill in blank
medium

Complete the code to enable Django to find templates inside app directories.

Django
TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': [1],
}]
Drag options to blanks, or click blank then click option'
ATrue
B'enabled'
CFalse
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Setting APP_DIRS to False disables app template loading
Using string values instead of boolean
3fill in blank
hard

Fix the error in the template directory path to use an absolute path.

Django
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, [1])],
    'APP_DIRS': True,
}]
Drag options to blanks, or click blank then click option'
A'templates/'
B'/templates/'
C'./templates/'
D'templates'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/templates/' which creates an absolute path ignoring BASE_DIR
Using './templates/' which is relative and may cause errors
4fill in blank
hard

Fill both blanks to configure Django to load templates from a custom directory and app directories.

Django
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, [1])],
    'APP_DIRS': [2],
}]
Drag options to blanks, or click blank then click option'
A'custom_templates'
BFalse
CTrue
D'templates'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'templates' instead of the custom folder name
Setting APP_DIRS to False disables app template loading
5fill in blank
hard

Fill all three blanks to correctly set up template directories and enable app templates in Django settings.

Django
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, [1])],
    'APP_DIRS': [2],
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            [3]
        ],
    },
}]
Drag options to blanks, or click blank then click option'
A'templates'
BTrue
C'django.contrib.auth.context_processors.auth'
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the auth context processor
Setting APP_DIRS to False
Using incorrect directory names

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