This is an enhancement of snippet #172. Here I use BeautifulSoup — far more easier to install through pip in a virtualenv, and possibly a bit more maintained — to format and properly indent the rendered HTML from templates.
I also added a check to only tidy contents in a
DEBUG=True environment, regarding high impact on performance while doing so in production.
Last, it's compatible with Django 1.2.x.
1 2 3 4 5 6 7 8 9 10 11
import settings from BeautifulSoup import BeautifulSoup class PrettifyMiddleware(object): """HTML code prettification middleware.""" def process_response(self, request, response): if settings.DEBUG and response['Content-Type'].split(';', 1) == 'text/html': response.content = BeautifulSoup(response.content).prettify() return response
More like this
- codigo alto nivel by MrRocklion 1 month ago
- Load template from specific app by Krzysiek555 1 month, 3 weeks ago
- PostgreSQL JSON subqueries by dolamroth 1 month, 3 weeks ago
- "Magic Link" Management Command by webology 7 months ago
- Closest ORM models to a latitude/longitude point by simonw 7 months ago