Skip to content
Django

Template Tags

Use built-in tags and filters in Django templates.

By EZ4Code Team
templatestagsfilters

Code

{% extends "base.html" %}
{% load static %}

{% block content %}
  <h1>{{ article.title|title }}</h1>
  <p>Published: {{ article.published_at|date:"Y-m-d" }}</p>

  {% if article.views > 100 %}
    <span class="hot">Hot</span>
  {% elif article.views > 10 %}
    <span>Warm</span>
  {% else %}
    <span>New</span>
  {% endif %}

  <ul>
    {% for tag in article.tags.all %}
      <li>{{ forloop.counter }}. {{ tag.name }}</li>
    {% empty %}
      <li>No tags</li>
    {% endfor %}
  </ul>

  <img src="{% static 'img/logo.png' %}" alt="Logo">
{% endblock %}

Explanation

Templates extend a base layout and override named blocks. Filters like title and date modify variables, while tags like if, for, and block control flow. The forloop object exposes loop metadata, and {% empty %} renders a fallback when the iterable is empty.

More Django Snippets