Performance: Why templates separate presentation
Separating templates from logic improves page load speed by reducing server processing time and enables efficient browser rendering.
def view(request): data = get_data() return render(request, 'template.html', {'items': data}) <!-- template.html --> <html><body>{% for item in items %}<div>{{ item.name }}</div>{% endfor %}</body></html>
def view(request): data = get_data() html = "<html><body>" for item in data: html += f"<div>{item['name']}</div>" html += "</body></html>" return HttpResponse(html)
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual string building in view | N/A (server-side) | N/A | N/A | [X] Bad |
| Using Django templates | N/A (server-side) | N/A | N/A | [OK] Good |
{% block content %}Base Content{% endblock %}{% extends 'base.html' %}{% block content %}Child Content{% endblock %}{% extends 'base.html' %}{% block content %}Hello{% endblock %}{% endblock %}Pass the test and this pattern moves unknown