Django Templates Flashcards
6 cards from real Django practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 Django Templates flashcards as text
Which template tag iterates over a list in Django?
Answer: {% for item in list %}
The {% for %} tag loops over each item in a sequence and must be closed with {% endfor %}.
What does `{{ forloop.counter }}` provide inside a Django for loop?
Answer: The 1-based index of the current loop iteration
forloop.counter gives the current iteration number starting at 1; use forloop.counter0 for a 0-based index.
How do you load a custom template tag library in Django?
Answer: {% load mytaglib %}
{% load %} at the top of a template loads a custom tag library from a templatetags directory, making its tags and filters available.
What is the `{% csrf_token %}` tag used for in Django forms?
Answer: Inserts a hidden CSRF token field to protect against cross-site request forgery
{% csrf_token %} renders a hidden input field with a CSRF token that Django's middleware validates on POST requests to prevent CSRF attacks.
Which template filter safely escapes HTML characters in Django?
Answer: escape
The escape filter converts characters like , &, and quotes to their HTML entity equivalents to prevent XSS; Django auto-escapes by default.
What does marking a variable with `{{ value|safe }}` do in Django templates?
Answer: Disables auto-escaping for that variable, rendering raw HTML
The safe filter tells Django's template engine to trust the variable's content and render it as raw HTML without escaping, which should only be used with trusted data.
Written and edited by Nathan Beja, founder of Practice Test Geeks, with AI assistance. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Editorial policy