Unit 3: Templates, Debugging and Testing - Practice Quiz

INT253 — Web Development In Python Using Django 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is the main purpose of a Django template?

Introduction to Templates in Django Easy
A. To define database tables
B. To present data as HTML
C. To start the development server
D. To configure URL routes

2 Which type of content is most commonly stored in a Django template?

Introduction to Templates in Django Easy
A. Python with model classes
B. JSON with server settings
C. HTML with template syntax
D. SQL with database queries

3 Which file extension is commonly used for Django template files?

Creating Templates Easy
A. .py
B. .html
C. .ini
D. .sql

4 Which directory name is commonly used to store Django template files?

Creating Templates Easy
A. media
B. static
C. templates
D. migrations

5 Which syntax displays a value in Django Template Language?

Working with Django Template Language (DTL) Easy
A. {# name #}
B. [[ name ]]
C. {% name %}
D. {{ name }}

6 Which symbol is used to apply a filter to a Django template variable?

Working with Django Template Language (DTL) Easy
A. &
B. @
C. #
D. |

7 Which delimiters are used for Django template tags?

Using template tags Easy
A. {{ and }}
B. {# and #}
C. <% and %>
D. {% and %}

8 Which Django template tag helps protect a form from cross-site request forgery?

Using template tags Easy
A. {% static %}
B. {% csrf_token %}
C. {% include %}
D. {% comment %}

9 How is a template variable named username displayed?

Django variables Easy
A. {{ username }}
B. {# username #}
C. < username >
D. {% username %}

10 How can a template access the name attribute of a variable called student?

Django variables Easy
A. {{ student/name }}
B. {{ student:name }}
C. {{ student->name }}
D. {{ student.name }}

11 Which tag correctly starts a loop over a list named products?

for loop and if-else statements Easy
A. {% for product in products %}
B. {% each product of products %}
C. {% while product in products %}
D. {% loop product from products %}

12 Which tag closes a Django template if statement?

for loop and if-else statements Easy
A. {% closeif %}
B. {% endif %}
C. {% stopif %}
D. {% finishif %}

13 What is normally passed from a Django view to a template to provide dynamic values?

Dynamic Templates in Django Easy
A. A migration file
B. A context dictionary
C. A URL pattern
D. A settings module

14 Which Django shortcut commonly combines a template with context data and returns a response?

Dynamic Templates in Django Easy
A. render()
B. include()
C. redirect()
D. reverse()

15 Which tag tells a child template to inherit from a parent template?

Working with Template inheritance Easy
A. {% include "base.html" %}
B. {% import "base.html" %}
C. {% require "base.html" %}
D. {% extends "base.html" %}

16 Which tag defines an area that child templates can replace?

Working with Template inheritance Easy
A. {% section content %}
B. {% area content %}
C. {% block content %}
D. {% region content %}

17 Which Django setting displays detailed error pages during development when enabled?

Debugging Django applications Easy
A. ALLOWED_HOSTS
B. DEBUG
C. LANGUAGE_CODE
D. STATIC_URL

18 Why should DEBUG normally be set to False in production?

Debugging Django applications Easy
A. To make database tables update automatically
B. To allow templates to inherit correctly
C. To prevent sensitive error details from appearing
D. To enable the Django administration site

19 Which command runs the test suite of a Django project?

Testing in Django Easy
A. python manage.py startapp
B. python manage.py test
C. python manage.py collectstatic
D. python manage.py migrate

20 Which Django class is commonly extended when writing test cases?

Testing in Django Easy
A. django.test.TestCase
B. django.views.TestCase
C. django.forms.TestCase
D. django.urls.TestCase

21 A view retrieves product data from the database. Which approach best follows Django's separation of concerns?

Introduction to Templates in Django Medium
A. Pass product data from the view to a template
B. Write database queries directly inside the template
C. Format the complete HTML inside the model
D. Store the rendered HTML in the URL configuration

22 Which statement correctly renders shop/product_list.html with a variable named products?

Introduction to Templates in Django Medium
A. return redirect(request, "shop/product_list.html", {"products": products})
B. return render(request, "shop/product_list.html", {"products": products})
C. return render("shop/product_list.html", request, products)
D. 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?

Creating Templates Medium
A. templates/details/app1.html
B. app1/templates/common/details.html
C. app1/templates/app1/details.html
D. 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?

Creating Templates Medium
A. INSTALLED_APPS["DIRS"]
B. TEMPLATES[0]["OPTIONS"]
C. TEMPLATES[0]["DIRS"]
D. MIDDLEWARE["TEMPLATES"]

25 A context variable title contains django testing. Which expression displays it as Django Testing?

Working with Django Template Language (DTL) Medium
A. {{ title|upper }}
B. {% title title %}
C. {{ title.capitalize }}
D. {{ 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?

Working with Django Template Language (DTL) Medium
A. The text appears in bold
B. The value is removed because Django permanently strips every HTML element before rendering the response
C. The variable causes a template error
D. The HTML tags appear as text

27 Given a URL pattern named product-detail that requires an integer pk, which template tag correctly generates the URL for product.pk?

Using template tags Medium
A. {% url 'product-detail' product.pk %}
B. {{ url 'product-detail' product.pk }}
C. {% link 'product-detail' product.pk %}
D. {% 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?

Using template tags Medium
A. {{ csrf_token() }}
B. {% load csrf %}
C. {% csrf_protect request %}
D. {% csrf_token %}

29 The view passes {"student": {"name": "Asha"}} as context. Which template expression displays Asha?

Django variables Medium
A. {{ student.name }}
B. {% student.name %}
C. {{ context.student.name }}
D. {{ student["name"] }}

30 A template contains Welcome, {{ username }}!, but username is absent from the context. What normally happens with Django's default template behavior?

Django variables Medium
A. Django automatically reads username from the authenticated user and displays it without requiring context
B. The variable renders as an empty string
C. The template raises KeyError immediately
D. The template displays the word None

31 Which DTL structure displays No products found only when the products collection is empty?

for loop and if-else statements Medium
A. {% for p in products %}{{ p.name }}{% else %}No products found{% endfor %}
B. {% if products %}No products found{% else %}{{ products }}{% endif %}
C. {% loop p in products %}{{ p.name }}{% empty %}No products found{% endloop %}
D. {% 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?

for loop and if-else statements Medium
A. {% if forloop.counter == 0 %}<hr>{% endif %}
B. {% if not forloop.last %}<hr>{% endif %}
C. {% if forloop.first %}<hr>{% endif %}
D. {% 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?

Dynamic Templates in Django Medium
A. Create a separate template for every possible name
B. Place the URL resolver inside the template so it can rerun the view and retrieve the captured name
C. Modify settings.py whenever the name changes
D. Pass the captured name to the template context

34 A view passes {"is_member": True} to a template. Which code dynamically shows Member Dashboard for members and Guest Dashboard otherwise?

Dynamic Templates in Django Medium
A. {{ if is_member }}Member Dashboard{{ else }}Guest Dashboard{{ endif }}
B. {% for is_member %}Member Dashboard{% empty %}Guest Dashboard{% endfor %}
C. {% if is_member %}Member Dashboard{% else %}Guest Dashboard{% endif %}
D. {% 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?

Working with Template inheritance Medium
A. {% extends 'base.html' %}{% block content %}Page content{% endblock %}
B. {% block 'base.html' %}{% extends content %}Page content{% endblock %}
C. {% inherit 'base.html' %}{% override content %}Page content{% endoverride %}
D. {% 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?

Working with Template inheritance Medium
A. {% extends sidebar.parent %}
B. {% include block.super %}
C. {{ parent.sidebar }}
D. {{ 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?

Debugging Django applications Medium
A. Set DEBUG = True in the development settings
B. Set DEBUG = True on the production server and expose the technical traceback publicly for easier diagnosis
C. Set ALLOWED_HOSTS = [] in every environment
D. Remove all middleware from the project settings

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?

Debugging Django applications Medium
A. Move the template into the migrations directory
B. Render templates/blog/posts_list.html instead
C. Add .django to the template filename
D. Render blog/posts_list.html instead

39 Which test correctly verifies that the URL named home returns HTTP status code ?

Testing in Django Medium
A. response = self.client.get(reverse('home')); self.assertEqual(response.status_code, 200)
B. response = self.client.get('home'); self.assertEqual(response, HttpResponse.status_ok)
C. response = reverse(self.client.get('home')); self.assertTrue(response.status_code)
D. 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?

Testing in Django Medium
A. self.assertRedirects(response, 'shop/product_list.html')
B. self.assertTemplateUsed(response, 'shop/product_list.html')
C. self.assertEqual(response.url, 'shop/product_list.html')
D. 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?

Introduction to Templates in Django Hard
A. render_to_response("dashboard.html", context, alias="admin")
B. render(request, "dashboard.html", context, using="admin")
C. render(request, "dashboard.html", context, engine="admin")
D. 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?

Creating Templates Hard
A. The file under BASE_DIR/templates
B. A TemplateDoesNotExist ambiguity error
C. The file under accounts/templates
D. The file under shop/templates

43 Given the context {"data": {"items": "dictionary value"}}, what does {{ data.items }} render in a Django template?

Working with Django Template Language (DTL) Hard
A. The dictionary's item pairs
B. dictionary value
C. An empty string
D. A template syntax error

44 The context contains an object report whose zero-argument method summary() returns "Ready". What normally happens when the template evaluates {{ report.summary }}?

Django variables Hard
A. DTL calls the method and renders Ready
B. DTL renders the bound method representation
C. DTL requires {{ report.summary() }}
D. DTL treats all methods as unavailable

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?

Django variables Hard
A. B|C
B. A|
C. A|C
D. 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?

Using template tags Hard
A. report_url becomes empty instead of raising NoReverseMatch
B. report_url receives the unresolved URL pattern text
C. report_url receives the current request path
D. 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 %}?

Using template tags Hard
A. theme as light plus user
B. Only theme, with the value light
C. All parent variables except user
D. Only 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?

for loop and if-else statements Hard
A. {{ forloop.counter0 }}
B. {{ parentloop.counter }}
C. {{ forloop.parentloop.counter }}
D. {{ 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 %}?

for loop and if-else statements Hard
A. A syntax error, because mixed operators are forbidden
B. Y, because DTL evaluates strictly left to right
C. Y, because or is evaluated before and
D. 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 %}?

for loop and if-else statements Hard
A. A queryset whose objects are false-like
B. A queryset containing None
C. A one-element list containing ""
D. An empty queryset

51 A view returns TemplateResponse(request, "report.html", context) and middleware must add context before rendering. Which statement accurately describes this response?

Dynamic Templates in Django Hard
A. Context processors run only when the view builds the response
B. Template rendering finishes before the view returns
C. Template rendering is deferred until response content is needed
D. Middleware cannot change the context after the view returns

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?

Dynamic Templates in Django Hard
A. Template.render() automatically strips authenticated users
B. Context exposes request data only when DEBUG=True
C. render() uses a request-aware context and configured processors
D. 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?

Working with Template inheritance Hard
A. The preceding HTML is automatically inserted into the first block
B. extends may appear anywhere if its argument is a string literal
C. The preceding HTML is prepended to the rendered parent template
D. 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?

Working with Template inheritance Hard
A. Report | {{ block.super }}
B. Default | Report
C. Report |
D. 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?

Working with Template inheritance Hard
A. A block name may be defined only once in one template
B. Block names must be globally unique across the project
C. A child may override only the final block in its parent
D. Repeated blocks are allowed only inside {% include %}

56 A developer sets DEBUG=True on a publicly reachable deployment to inspect an exception. Which statement identifies the primary Django-specific risk?

Debugging Django applications Hard
A. CSRF middleware is disabled for every unsafe request
B. Database queries stop using parameter binding while debugging
C. Django automatically grants staff permissions to all visitors
D. Technical error pages may expose settings, paths, and request data

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?

Debugging Django applications Hard
A. Mark the value by calling mark_safe(password)
B. Decorate the view with @require_POST("password")
C. Decorate the view with @sensitive_post_parameters("password")
D. Wrap the view with @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?

Testing in Django Hard
A. The default client adds a valid token; use Client(csrf_checks=False)
B. The default client disables middleware; use RequestFactory() instead
C. The default client converts POST to GET; use AsyncClient() instead
D. The default client skips CSRF checks; use 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?

Testing in Django Hard
A. Use captureOnCommitCallbacks() or a TransactionTestCase
B. Call transaction.commit() directly inside the TestCase
C. Enable DEBUG=True before registering the callback
D. Replace 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?

Testing in Django Hard
A. TestCase.setUp()
B. TestCase.setUpTestData()
C. TransactionTestCase.tearDown()
D. SimpleTestCase.setUpClass()