Cursor Rules for
Django Template Language

This rule explains Django Template Language syntax and best practices.
Back to rules
Type
Frontend
Language(s)
Python
Used with
Stats
13 views
0 copies
0 downloads
django-dtl.mdc
---
description: This rule explains Django Template Language syntax and best practices.
globs: *.html
alwaysApply: false
---

# Django DTL rules

- Use template inheritance for layouts:

```html
<!-- base.html -->
<!DOCTYPE html>
<html>
<head>
  <title>{% block title %}Default Title{% endblock %}</title>
</head>
<body>
  {% block content %}{% endblock %}
</body>
</html>
```

- Use include for components:

```html
{% include "components/user_card.html" with user=current_user %}
```

- Use template tags for logic:

```html
{% if user.is_authenticated %}
  <p>Welcome, {{ user.username }}!</p>
{% else %}
  <p>Please log in</p>
{% endif %}
```

- Use filters to format data:

```html
<p>{{ text|truncatewords:30 }}</p>
<p>{{ date|date:"F j, Y" }}</p>
```

- Create custom template tags:

```python
@register.filter
def currency(value):
    return f"${value:.2f}"
```

- Use url and static tags:

```html
<a href="{% url 'view_name' %}">Link</a>
{% load static %}
<img src="{% static 'images/logo.png' %}" alt="Logo">
```