Template Tags Example

A snippet showing how to use Django's built-in template tags.


# models.py

from django.db import models

class Item(models.Model):
    name = models.CharField(max_length=100)
    stock = models.PositiveIntegerField(default=0)

    def __str__(self):
        return self.name
      

# admin.py

from django.contrib import admin
from .models import Item

@admin.register(Item)
class ItemAdmin(admin.ModelAdmin):
    list_display = ("name", "stock")
      

# templates/item_detail.html

{% if item.stock %}
  In stock
{% else %}
  Out of stock
{% endif %}

{% for i in range %}
  {{ i }}
{% endfor %}

{% block content %}{% endblock %}

{% include "partials/card.html" %}
      
Explanation:
  • Core tags cover flow control (if, for), structure (block), and composition (include).
  • Tags operate on context variables; avoid heavy business logic in templates.
  • Category Templates
  • Total Views 1155
  • Last Modified 26 January, 2026
  • Tags #templates #tags #logic #blocks
Previous snippet
Custom Template Filters
Next snippet
Custom Template Tags
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.