0
0
Djangoframework~20 mins

Template configuration and directories in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding TEMPLATE_DIRS setting in Django
In a Django project, what is the purpose of the TEMPLATE_DIRS setting in settings.py?
AIt defines the list of directories where Django looks for template files.
BIt sets the database connection strings for template rendering.
CIt specifies the URL paths for static files like CSS and JavaScript.
DIt controls the middleware order for processing template requests.
Attempts:
2 left
💡 Hint
Think about where Django finds the HTML files it uses to build pages.
component_behavior
intermediate
1:30remaining
Effect of missing template directory in settings
If you forget to add your custom template directory to the DIRS list inside the TEMPLATES setting in settings.py, what will happen when you try to render a template located there?
ADjango will use the default template from the admin app instead.
BDjango will render a blank page without any error.
CDjango will raise a TemplateDoesNotExist error because it cannot find the template.
DDjango will automatically search all project folders and find the template anyway.
Attempts:
2 left
💡 Hint
Think about how Django knows where to look for templates.
📝 Syntax
advanced
2:00remaining
Correct syntax for adding a template directory in Django 4+
Which of the following is the correct way to add a custom template directory named templates located at the project base directory in Django 4+ settings.py?
ATEMPLATES = [{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': 'BASE_DIR/templates', 'APP_DIRS': True, 'OPTIONS': {} }]
BTEMPLATES = [{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR / 'templates'], 'APP_DIRS': True, 'OPTIONS': {} }]
CTEMPLATES = [{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': BASE_DIR + '/templates', 'APP_DIRS': True, 'OPTIONS': {} }]
DTEMPLATES = [{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ['BASE_DIR/templates'], 'APP_DIRS': True, 'OPTIONS': {} }]
Attempts:
2 left
💡 Hint
Remember that BASE_DIR is a Path object and you should use the division operator to join paths.
🔧 Debug
advanced
2:00remaining
Diagnosing template loading failure in Django
You have this settings.py snippet:
TEMPLATES = [{
  'BACKEND': 'django.template.backends.django.DjangoTemplates',
  'DIRS': ['templates'],
  'APP_DIRS': True,
  'OPTIONS': {},
}]

Your template is located at /home/user/myproject/templates/index.html. When rendering, Django raises TemplateDoesNotExist. What is the most likely cause?
AThe <code>DIRS</code> path 'templates' is relative and does not point to the absolute template folder location.
BThe <code>APP_DIRS</code> option must be set to False to load templates from <code>DIRS</code>.
CThe template filename must be <code>index.htm</code> instead of <code>index.html</code>.
DThe <code>BACKEND</code> setting is incorrect and should be <code>django.template.backends.jinja2.Jinja2</code>.
Attempts:
2 left
💡 Hint
Check if the path in DIRS matches the actual folder location.
state_output
expert
2:30remaining
Number of template directories searched by Django
Given this settings.py snippet:
BASE_DIR = Path(__file__).resolve().parent.parent

TEMPLATES = [{
  'BACKEND': 'django.template.backends.django.DjangoTemplates',
  'DIRS': [BASE_DIR / 'custom_templates'],
  'APP_DIRS': True,
  'OPTIONS': {},
}]

And you have 3 Django apps each with a templates folder inside their app directory. How many directories will Django search for templates when rendering?
ANone, because <code>OPTIONS</code> is empty.
BOnly 1 directory: the one specified in <code>DIRS</code>.
C3 directories: only the app <code>templates</code> folders because <code>APP_DIRS</code> is True.
D4 directories: the one in <code>DIRS</code> plus the 3 app <code>templates</code> folders.
Attempts:
2 left
💡 Hint
Remember what APP_DIRS does in Django template settings.