Unit 3: Templates, Debugging and Testing - Practice Quiz
1 What is the main purpose of a Django template?
2 Which type of content is most commonly stored in a Django template?
3 Which file extension is commonly used for Django template files?
.py
.html
.ini
.sql
4 Which directory name is commonly used to store Django template files?
media
static
templates
migrations
5 Which syntax displays a value in Django Template Language?
{# name #}
[[ name ]]
{% name %}
{{ name }}
6 Which symbol is used to apply a filter to a Django template variable?
&
@
#
|
7 Which delimiters are used for Django template tags?
{{ and }}
{# and #}
<% and %>
{% and %}
8 Which Django template tag helps protect a form from cross-site request forgery?
{% static %}
{% csrf_token %}
{% include %}
{% comment %}
9
How is a template variable named username displayed?
{{ username }}
{# username #}
< username >
{% username %}
10
How can a template access the name attribute of a variable called student?
{{ student/name }}
{{ student:name }}
{{ student->name }}
{{ student.name }}
11
Which tag correctly starts a loop over a list named products?
{% for product in products %}
{% each product of products %}
{% while product in products %}
{% loop product from products %}
12
Which tag closes a Django template if statement?
{% closeif %}
{% endif %}
{% stopif %}
{% finishif %}
13 What is normally passed from a Django view to a template to provide dynamic values?
14 Which Django shortcut commonly combines a template with context data and returns a response?
render()
include()
redirect()
reverse()
15 Which tag tells a child template to inherit from a parent template?
{% include "base.html" %}
{% import "base.html" %}
{% require "base.html" %}
{% extends "base.html" %}
16 Which tag defines an area that child templates can replace?
{% section content %}
{% area content %}
{% block content %}
{% region content %}
17 Which Django setting displays detailed error pages during development when enabled?
ALLOWED_HOSTS
DEBUG
LANGUAGE_CODE
STATIC_URL
18
Why should DEBUG normally be set to False in production?
19 Which command runs the test suite of a Django project?
python manage.py startapp
python manage.py test
python manage.py collectstatic
python manage.py migrate
20 Which Django class is commonly extended when writing test cases?
django.test.TestCase
django.views.TestCase
django.forms.TestCase
django.urls.TestCase
21 A view retrieves product data from the database. Which approach best follows Django's separation of concerns?
22
Which statement correctly renders shop/product_list.html with a variable named products?
return redirect(request, "shop/product_list.html", {"products": products})
return render(request, "shop/product_list.html", {"products": products})
return render("shop/product_list.html", request, products)
return HttpResponse(request, "shop/product_list.html", products)
23
Two installed applications both contain a template named details.html. Which directory structure best prevents template-name collisions?
templates/details/app1.html
app1/templates/common/details.html
app1/templates/app1/details.html
app1/details/templates.html
24
A template is stored at project_templates/dashboard.html, outside all Django applications. Which setting is normally updated so Django can locate it?
INSTALLED_APPS["DIRS"]
TEMPLATES[0]["OPTIONS"]
TEMPLATES[0]["DIRS"]
MIDDLEWARE["TEMPLATES"]
25
A context variable title contains django testing. Which expression displays it as Django Testing?
{{ title|upper }}
{% title title %}
{{ title.capitalize }}
{{ title|title }}
26
The variable comment contains the text <strong>Great</strong> submitted by a user. What does {{ comment }} normally display when autoescaping is enabled?
27
Given a URL pattern named product-detail that requires an integer pk, which template tag correctly generates the URL for product.pk?
{% url 'product-detail' product.pk %}
{{ url 'product-detail' product.pk }}
{% link 'product-detail' product.pk %}
{% url product.pk as 'product-detail' %}
28
A form submits data with method="post" to a Django view protected by CSRF middleware. What should be placed inside the <form> element?
{{ csrf_token() }}
{% load csrf %}
{% csrf_protect request %}
{% csrf_token %}
29
The view passes {"student": {"name": "Asha"}} as context. Which template expression displays Asha?
{{ student.name }}
{% student.name %}
{{ context.student.name }}
{{ student["name"] }}
30
A template contains Welcome, {{ username }}!, but username is absent from the context. What normally happens with Django's default template behavior?
username from the authenticated user and displays it without requiring context
KeyError immediately
None
31
Which DTL structure displays No products found only when the products collection is empty?
{% for p in products %}{{ p.name }}{% else %}No products found{% endfor %}
{% if products %}No products found{% else %}{{ products }}{% endif %}
{% loop p in products %}{{ p.name }}{% empty %}No products found{% endloop %}
{% for p in products %}{{ p.name }}{% empty %}No products found{% endfor %}
32
Inside a loop over orders, which condition adds a separator after every order except the last one?
{% if forloop.counter == 0 %}<hr>{% endif %}
{% if not forloop.last %}<hr>{% endif %}
{% if forloop.first %}<hr>{% endif %}
{% if orders.last != order %}<hr>{% endif %}
33 A view must display a different greeting based on a name captured from the URL. Which implementation is most appropriate?
settings.py whenever the name changes
34
A view passes {"is_member": True} to a template. Which code dynamically shows Member Dashboard for members and Guest Dashboard otherwise?
{{ if is_member }}Member Dashboard{{ else }}Guest Dashboard{{ endif }}
{% for is_member %}Member Dashboard{% empty %}Guest Dashboard{% endfor %}
{% if is_member %}Member Dashboard{% else %}Guest Dashboard{% endif %}
{% if is_member == "True" %}Member Dashboard{% otherwise %}Guest Dashboard{% endif %}
35
A child template extends base.html and needs to replace its content block. Which structure is correct?
{% extends 'base.html' %}{% block content %}Page content{% endblock %}
{% block 'base.html' %}{% extends content %}Page content{% endblock %}
{% inherit 'base.html' %}{% override content %}Page content{% endoverride %}
{% include 'base.html' %}{% content %}Page content{% endcontent %}
36
A child template overrides the sidebar block but must retain the parent's sidebar before adding new links. What should the child block use?
{% extends sidebar.parent %}
{% include block.super %}
{{ parent.sidebar }}
{{ block.super }}
37 A page raises an exception during local development, but the browser only shows a generic server error. Which configuration is most directly relevant for displaying Django's detailed debug page?
DEBUG = True in the development settings
DEBUG = True on the production server and expose the technical traceback publicly for easier diagnosis
ALLOWED_HOSTS = [] in every environment
38
A template raises TemplateDoesNotExist: blog/post_list.html. The file is actually stored as blog/templates/blog/posts_list.html. What is the most direct fix?
templates/blog/posts_list.html instead
.django to the template filename
blog/posts_list.html instead
39
Which test correctly verifies that the URL named home returns HTTP status code ?
response = self.client.get(reverse('home')); self.assertEqual(response.status_code, 200)
response = self.client.get('home'); self.assertEqual(response, HttpResponse.status_ok)
response = reverse(self.client.get('home')); self.assertTrue(response.status_code)
response = self.client.post(reverse('home')); self.assertEqual(response.template, 200)
40
A test must confirm that requesting a product-list page uses shop/product_list.html. Which assertion is designed for this purpose?
self.assertRedirects(response, 'shop/product_list.html')
self.assertTemplateUsed(response, 'shop/product_list.html')
self.assertEqual(response.url, 'shop/product_list.html')
self.assertContains(response, 'shop/product_list.html')
41
A project configures two Django template engines with aliases public and admin. Both engines can resolve dashboard.html. A view must render the template from the admin engine regardless of template search order. Which call provides that guarantee?
render_to_response("dashboard.html", context, alias="admin")
render(request, "dashboard.html", context, using="admin")
render(request, "dashboard.html", context, engine="admin")
render(request, "admin:dashboard.html", context)
42
A project uses the Django template backend with DIRS = [BASE_DIR / "templates"], APP_DIRS = True, and INSTALLED_APPS = ["shop", "accounts"]. The same receipt.html exists in the project template directory and in both applications' templates directories. What does get_template("receipt.html") load?
BASE_DIR/templates
TemplateDoesNotExist ambiguity error
accounts/templates
shop/templates
43
Given the context {"data": {"items": "dictionary value"}}, what does {{ data.items }} render in a Django template?
dictionary value
44
The context contains an object report whose zero-argument method summary() returns "Ready". What normally happens when the template evaluates {{ report.summary }}?
Ready
{{ report.summary() }}
45
A context variable is missing, and the engine uses its default string_if_invalid value. Consider {% if missing %}A{% else %}B{% endif %}|{{ missing|default:"C" }}. What is rendered?
B|C
A|
A|C
B|
46
Suppose {% url "reports:detail" missing_id as report_url %} cannot reverse the URL because missing_id is invalid. What is the key effect of assigning the result with as report_url?
report_url becomes empty instead of raising NoReverseMatch
report_url receives the unresolved URL pattern text
report_url receives the current request path
report_url becomes None and the exception is logged
47
A parent context has theme="dark" and user. What variables are intentionally available to row.html when it is rendered by {% include "row.html" with theme="light" only %}?
theme as light plus user
theme, with the value light
user
theme, with the value dark
48 An outer loop iterates over two categories, and each category has three products. Inside the inner loop, which expression renders the current category's one-based position rather than the product's position?
{{ forloop.counter0 }}
{{ parentloop.counter }}
{{ forloop.parentloop.counter }}
{{ forloop.parent.counter }}
49
For a=True, b=False, and c=False, which branch is selected by {% if a or b and c %}X{% else %}Y{% endif %}?
Y, because DTL evaluates strictly left to right
Y, because or is evaluated before and
X, because and is evaluated before or
50
A template uses {% for item in items %}{{ item }}{% empty %}No items{% endfor %}. Which input causes the empty branch to run without requiring a separate {% if %}?
None
""
51
A view returns TemplateResponse(request, "report.html", context) and middleware must add context before rendering. Which statement accurately describes this response?
52
A template rendered with render(request, "page.html", context) can access request.user, but the same template rendered with Template("...").render(Context(context)) cannot. What best explains the difference?
Template.render() automatically strips authenticated users
Context exposes request data only when DEBUG=True
render() uses a request-aware context and configured processors
render() copies user data directly into every context
53
A child template contains visible HTML before {% extends "base.html" %}. What is the correct diagnosis under Django template inheritance rules?
extends may appear anywhere if its argument is a string literal
extends must be the first template tag to control inheritance
54
A base template defines {% block title %}Default{% endblock %}. A child overrides it with {% block title %}Report | {{ block.super }}{% endblock %}. What does the child render for that block?
Report | {{ block.super }}
Default | Report
Report |
Report | Default
55
A child template attempts to define two blocks named content at the same inheritance level because the base appears to use that block in two places. Why is this invalid?
{% include %}
56
A developer sets DEBUG=True on a publicly reachable deployment to inspect an exception. Which statement identifies the primary Django-specific risk?
57 A view accepts a password parameter and may raise an exception. Which Django mechanism is designed to prevent that parameter's value from appearing in technical error reports?
mark_safe(password)
@require_POST("password")
@sensitive_post_parameters("password")
@csrf_protect("password")
58
A test must verify that a POST endpoint rejects requests lacking a CSRF token. Why can a default django.test.Client produce a misleading result, and what should the test use?
Client(csrf_checks=False)
RequestFactory() instead
AsyncClient() instead
Client(enforce_csrf_checks=True)
59
A TestCase registers a callback with transaction.on_commit() and expects the callback to run normally before the test method ends, but it never does. What is the most appropriate testing approach?
captureOnCommitCallbacks() or a TransactionTestCase
transaction.commit() directly inside the TestCase
DEBUG=True before registering the callback
on_commit() with setUpTestData()
60 A test class needs database objects created once for the class and then available with isolated in-memory field state in each test method. Which Django testing feature is intended for this?
TestCase.setUp()
TestCase.setUpTestData()
TransactionTestCase.tearDown()
SimpleTestCase.setUpClass()
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →