Trivia: Achieving neater HTML renditions

This isn’t affecting web clients (browsers) and is probably trivial. Perhaps I am being too fussy but here it goes …

I find that Django replaces its markup (DTL, Django Template Language) with spaces on the final HTML output. Sometimes the spaces are horrendous depending on the stacking of conditional phrases or indented loops. The resulting HTML is often difficult to read from a human standpoint.

Is this something users should learn to endure or is there a setting for achieving neater HTML outputs from DTL?

what space? need example.

You can get rid of a lot of the excess spaces by using the spaceless tag. It’s still not going to give you a nicely formatted layout, but does condense it a bit.

Although, to be honest, it’s been a long time since I’ve bothered looking at the raw html directly. I’m almost always examining it using the browser’s developer tools, which ignores the original formatting.

To answer your direct question - yes there are times when it’s extremely helpful to be able to read the raw html, usually in a troubleshooting situation. But that should be the exception and not the rule. In general, I stopped worrying about the formatting of html the first time I started working with a CMS around 2003/2004.

1 Like

Ah, that is nice although I prefer indented tags. It does condense it … may be a tad too much. Okay, I will take your advice and ignore the outputs. I was able to make it somewhat bearable but I am not sure my approach would be considered standard in team settings.

Rather than:

{% if condition %}
<div>
    <p>text here</p>
</div>
{% endif %}

I end up with:

{% if condition %}<div>
    <p>text here</p>
</div>{% endif %}

The final output is as desired but this trick only works in select cases (not holistic). It does not fix all.

1 Like