Sample questions
Question 1Easy
Which tag is used for a loop in the Django template language?
- {% loop item in items %} ... {% endloop %}
- {% foreach items as item %} ... {% endforeach %}
- {% for item in items %} ... {% endfor %}
- {{ for item in items }} ... {{ endfor }}
Answer: C. {% for item in items %} ... {% endfor %}
Explanation
for tags require endfor.
Step-by-step solution
- Django templates use {% for %} to start a loop.
- {% endfor %} is needed to close the loop.
- Option C uses both {% for %} and {% endfor %}.
- Other options use incorrect syntax like {{ for }} or wrong tags.
- 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?
- {% loop items %}
- {{ for items }}
- {% for item in items %}
- {% each items %}
Answer: C. {% for item in items %}
Explanation
Close with {% endfor %}; {% empty %} handles empty lists.
Step-by-step solution
- Open for.
- 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?
- {% name %}
- <% name %>
- {{ name }}
- ${name}
Answer: C. {{ name }}
Explanation
Double braces output variables; {% %} runs tags.
Step-by-step solution
- Use double braces.
- 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?
- {% url 'detail' pk=3 %}
- {% route 'detail' %}
- {{ url.detail }}
- {% link 'detail' %}
Answer: A. {% url 'detail' pk=3 %}
Explanation
The url tag calls reverse using the pattern name and arguments.
Step-by-step solution
- Name the route.
- 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?
- HTML special characters are automatically escaped
- They are not escaped unless the template author adds the |escape filter explicitly
- Only quotes are escaped
- 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
- Variable output.
- 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?
- Always shows N/A for every value, whether or not the variable has a value
- Sets the variable
- Raises an error if missing
- 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
- Value evaluated.
- 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?
- {% csrf_token %}
- {% xsrf %}
- {% secure_form %}
- {% csrf_protect %}
Answer: A. {% csrf_token %}
Explanation
The token is checked by the CsrfViewMiddleware.
Step-by-step solution
- POST forms need a CSRF token to prevent forgery.
- Django uses {% csrf_token %} for this purpose.
- Other tags like {% xsrf %} are not standard in Django.
- The correct tag is {% csrf_token %}.
- 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' %}?
- To import Python code from another module into the template for use in loops
- To inherit a parent template and override its blocks
- To include CSS
- 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
- Define blocks in base.
- 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?
- Encrypts the whole form payload before the browser sends it to the server
- Logs the user in
- Sets a cookie for language
- 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
- Add tag.
- Token posted.
- 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?
- It passes them as a list taken from the filter chain following the method name
- Django templates cannot pass arguments to methods; use a filter or compute it in the view
- It raises syntax error always
- 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
- Template calls without args.
- 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 →