🎸 Django Beginner

What is Django Template Language (DTL)?

Answer

Django Template Language (DTL) is Django's built-in template engine for rendering HTML with dynamic data. Variables: {{ variable }} — renders the value; {{ user.username }} — attribute access; {{ dictionary.key }} — dict access; {{ list.0 }} — index access. Tags: {% if condition %}...{% elif %}...{% else %}...{% endif %}; {% for item in items %}{{ item.name }}{% empty %}No items{% endfor %}; {% url "view-name" arg1 arg2 %} — URL generation; {% include "partial.html" %} — include template; {% extends "base.html" %} — template inheritance; {% block content %}...{% endblock %} — define block for child templates; {% load staticfiles %}{% static "css/style.css" %} — static files; {% csrf_token %} — CSRF protection in forms. Filters: {{ name|lower }}; {{ text|truncatewords:30 }}; {{ date|date:"M d, Y" }}; {{ price|floatformat:2 }}; {{ html_content|safe }} — mark as safe (no escaping); {{ items|length }}; {{ value|default:"N/A" }}. Template inheritance: base.html defines blocks; child templates extend and fill them: {% extends "base.html" %}{% block title %}Article{% endblock %}{% block content %}<h1>{{ article.title }}</h1>{% endblock %}. DTL auto-escapes HTML by default — prevents XSS. Alternative: Jinja2 is also supported.