6đź‘Ť
For a bit of background, this is done intentionally, since Django’s templating engine currently doesn’t care about sanitizing lines that evaluate to blanks after the tags get stripped out and doing so will incur some minor performance penalties in serving the response, since it involves a post-processing step that evaluates the full contents of the rendered template.
Still, if you need a quick solution, I suggest you employ the StripWhitespaceMiddleware
response middleware that’ll remove spurious whitespaces from textual responses. It’s pretty fast and straightforward, employing regular expression matching like the templating engine itself. A more taxing, but powerful alternative would be deploy a response middleware that utilizes an HTML prettifier, if you really care about the poor human beings still reading raw responses.
4đź‘Ť
django-htmlmin package is a awesome, it provides a lot of control and features moreover it supports HTML 5.
- Django python-rq — DatabaseError SSL error: decryption failed or bad record mac
- Django – links generated with {% url %} – how to make them secure?
- Deploying existing Django app on Heroku
- Django: datetime filter by date ignoring time
2đź‘Ť
Looking around I just found django-pipeline, I just tried, by now, the middleware and it’s just what I’m expected. It minify all html and text nodes and keep javascript nodes untouched.
Middleware implementation and basic test result is about 4 minutes, just pip install and activate the middleware after you gzip middleware or on top of your MIDDLEWARE_CLASSES setting.
- Pycharm error: Improperly configured
- Filter a Django form select element based on a previously selected element
2đź‘Ť
I think a front-end development tool, such as grunt-contrib-htmlmin
, should be used to minify Django template files before deploying them to production server.
This way, the minification process happens in “build-time”, and saves server CPU resource for HTML minification in “run-time”.
This tool is originally designed for minifying HTML, but also works for Django templates as I tried out.
- Django Celery Task Logging
- Django TransactionManagementError when using signals
- Django's syncdb fails with MySQL errno: 150
- Unsupported lookup 'istartwith' for CharField or join on the field not permitted
1đź‘Ť
If you’re using with Nginx its Strip module is doing really great job with cleaning the html for you. It’s real time and transparent. You don’t need to care about at all after you set it up correctly.
1đź‘Ť
“I think a front-end development tool, such as grunt-contrib-htmlmin, should be used to minify Django template files before deploying them to production server.”
I had a need to do this some time ago to increase performance and decrease latency. I wrote a template minimizer that will minimize the HTML, CSS, and Javascript in your templates beforehand so that the pages are composed minimized. It integrates into Django as a Django command, understands the intricacies of the Django tempting language, and is extendable. You can find it on pypi at the link below:
- Django no module named "compressor"
- Send email confirmation after registration django
- Django-filter with DRF – How to do 'and' when applying multiple values with the same lookup?
- Django, apache, mod_wsgi – Error: Premature end of script headers
0đź‘Ť
I recommend using a custom template loader django-spaceless-templates, which strips unnecessary whitespace characters. This approach has two advantages:
- With the combination with cached template loader, whitespace stripping is done only once during template compilation. On the other hand with {% spaceless %} or middleware approach minification is called every time a template is rendered.
- Whitespace stripping can be more aggressive – remove spaces between HTML tags, Django tags and variables.
- Using django-rest-interface with http put
- Fail to push to Heroku: /app/.heroku/python/bin/pip:No such file or directory
- Django: foreign key value in a list display admin
- Django DateTimeField says 'You are 5.5 hours ahead of server time.'