Performance: Displaying forms in templates
MEDIUM IMPACT
This affects page load speed and rendering performance by how form HTML is generated and inserted into the page.
{{ form.as_p }}{% for field in form %}
<div>{{ field.label_tag }} {{ field }}</div>
{% endfor %}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Loop rendering each field | Many small insertions | Multiple reflows (one per field) | Higher paint cost due to layout thrashing | [X] Bad |
| Render form as single block (e.g. form.as_p) | Single insertion | Single reflow | Lower paint cost, smoother rendering | [OK] Good |