Templates Questions with Answers

14 practice questions on Templates (Django) with answers and step-by-step solutions. Try 10 samples below, then practise all 14 free in the AptiNest app.

Sample questions

Question 1Easy

Which tag is used for a loop in the Django template language?

  1. {% loop item in items %} ... {% endloop %}
  2. {% foreach items as item %} ... {% endforeach %}
  3. {% for item in items %} ... {% endfor %}
  4. {{ for item in items }} ... {{ endfor }}

Answer: C. {% for item in items %} ... {% endfor %}

Explanation

for tags require endfor.

Step-by-step solution

  1. Django templates use {% for %} to start a loop.
  2. {% endfor %} is needed to close the loop.
  3. Option C uses both {% for %} and {% endfor %}.
  4. Other options use incorrect syntax like {{ for }} or wrong tags.
  5. Final answer: {% for item in items %} ... {% endfor %}.

Why the other options are wrong

  • A. loop is not a Django template tag; the tag is for, closed by endfor.
  • B. foreach is not a Django tag; the loop tag is for with endfor.
  • D. Double braces are for output only; loop tags need the {% %} delimiters.

Tip: Remember to always close loops with {% endfor %} in Django templates.

Question 2Easy

Which tag loops over a list in a template?

  1. {% loop items %}
  2. {{ for items }}
  3. {% for item in items %}
  4. {% each items %}

Answer: C. {% for item in items %}

Explanation

Close with {% endfor %}; {% empty %} handles empty lists.

Step-by-step solution

  1. Open for.
  2. Close endfor.

Why the other options are wrong

  • A. {% loop items %} is not a Django template tag; the correct choice is {% for item in items %}.
  • B. Variables syntax does not loop.
  • D. {% each items %} is not a Django template tag; the correct choice is {% for item in items %}.

Question 3Easy

Which syntax prints a variable name in a Django template?

  1. {% name %}
  2. <% name %>
  3. {{ name }}
  4. ${name}

Answer: C. {{ name }}

Explanation

Double braces output variables; {% %} runs tags.

Step-by-step solution

  1. Use double braces.
  2. Value is auto-escaped.

Why the other options are wrong

  • A. Tags do logic, not variable output.
  • B. EJS style.
  • D. JS template literal.

Question 4Easy

Which tag outputs the URL of a named route?

  1. {% url 'detail' pk=3 %}
  2. {% route 'detail' %}
  3. {{ url.detail }}
  4. {% link 'detail' %}

Answer: A. {% url 'detail' pk=3 %}

Explanation

The url tag calls reverse using the pattern name and arguments.

Step-by-step solution

  1. Name the route.
  2. Pass args.

Why the other options are wrong

  • B. {% route 'detail' %} is not a Django template tag; the correct choice is {% url 'detail' pk=3 %}.
  • C. {{ url.detail }} is plain variable syntax and cannot reverse a URL name; the url tag does that.
  • D. {% link 'detail' %} is not a Django template tag; the correct choice is {% url 'detail' pk=3 %}.

Question 5Medium

How are variables escaped in Django templates by default?

  1. HTML special characters are automatically escaped
  2. They are not escaped unless the template author adds the |escape filter explicitly
  3. Only quotes are escaped
  4. Escaped only in forms

Answer: A. HTML special characters are automatically escaped

Explanation

This protects against XSS; use |safe only for trusted content.

Step-by-step solution

  1. Variable output.
  2. Autoescape applies.

Why the other options are wrong

  • B. Autoescape is on.
  • C. All of <, >, &, quotes are escaped.
  • D. Applies to all templates.

Tip: Never mark user input safe.

Question 6Medium

What does {{ value|default:'N/A' }} do?

  1. Always shows N/A for every value, whether or not the variable has a value
  2. Sets the variable
  3. Raises an error if missing
  4. Shows N/A when value is false-like

Answer: D. Shows N/A when value is false-like

Explanation

The default filter applies to falsy values such as None or empty string.

Step-by-step solution

  1. Value evaluated.
  2. Falsy gives default.

Why the other options are wrong

  • A. Only for false values.
  • B. Filters do not assign.
  • C. Missing variables render empty.

Question 7Medium

Which form tag is required in a POST form to protect against cross-site request forgery?

  1. {% csrf_token %}
  2. {% xsrf %}
  3. {% secure_form %}
  4. {% csrf_protect %}

Answer: A. {% csrf_token %}

Explanation

The token is checked by the CsrfViewMiddleware.

Step-by-step solution

  1. POST forms need a CSRF token to prevent forgery.
  2. Django uses {% csrf_token %} for this purpose.
  3. Other tags like {% xsrf %} are not standard in Django.
  4. The correct tag is {% csrf_token %}.
  5. Final answer: {% csrf_token %}.

Why the other options are wrong

  • B. xsrf is not a Django template tag; the CSRF tag is csrf_token.
  • C. secure_form is not a Django tag; csrf_token is the form tag that protects POST.
  • D. csrf_protect is not a template tag; the template tag is csrf_token.

Tip: Use {% csrf_token %} in POST forms for CSRF protection.

Question 8Medium

What is the purpose of {% extends 'base.html' %}?

  1. To import Python code from another module into the template for use in loops
  2. To inherit a parent template and override its blocks
  3. To include CSS
  4. To load static

Answer: B. To inherit a parent template and override its blocks

Explanation

It must be the first tag; child templates fill {% block %} sections.

Step-by-step solution

  1. Define blocks in base.
  2. Override in child.

Why the other options are wrong

  • A. Not Python.
  • C. Not CSS.
  • D. {% load static %} does that.

Question 9Hard

What does {% csrf_token %} do inside a POST form?

  1. Encrypts the whole form payload before the browser sends it to the server
  2. Logs the user in
  3. Sets a cookie for language
  4. Inserts a hidden field checked by CsrfViewMiddleware

Answer: D. Inserts a hidden field checked by CsrfViewMiddleware

Explanation

The middleware compares the token with the CSRF cookie and rejects mismatches with 403.

Step-by-step solution

  1. Add tag.
  2. Token posted.
  3. Middleware verifies.

Why the other options are wrong

  • A. It does not encrypt.
  • B. Login is separate.
  • C. Language cookie is unrelated.

Question 10Hard

What happens when a template tries to call a method that needs arguments?

  1. It passes them as a list taken from the filter chain following the method name
  2. Django templates cannot pass arguments to methods; use a filter or compute it in the view
  3. It raises syntax error always
  4. It passes None

Answer: B. Django templates cannot pass arguments to methods; use a filter or compute it in the view

Explanation

Templates call callables without arguments; keep logic in views/models.

Step-by-step solution

  1. Template calls without args.
  2. Move logic out.

Why the other options are wrong

  • A. Templates cannot pass arguments to a method, whether through a filter chain or otherwise.
  • C. The call silently fails or uses no args.
  • D. Django calls the method with no arguments; it does not pass None as an argument.

Practise all 14 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Django topics · All topics