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
Model Definition
Define a Django model with field types and meta options.
ORM QuerySet
Filter, exclude, annotate and chain QuerySets.
Class-Based Views
Use generic class-based views for common CRUD flows.
URL Routing
Wire URLs to views with path converters and includes.
ModelForms
Build forms from models with validation and widgets.
Admin Customization
Customize the Django admin with list_display and actions.