16👍
Christian S. Perone from Pyevolve has exactly what you’re looking for. He uses middleware to intercept the HTTPResponse object and run it through BeautifulSoup.
I’d imagine that your site will suffer a slight performance hit, though. I recommend running some benchmarks before deploying this middleware to a production site.
4👍
You can override the NodeList’s render method as I’ve done. See my question with working code:
Proper indentation in Django templates (without monkey-patching)?
- Send email to bcc and cc in django
- Django cascade delete on reverse foreign keys
- Django DB level default value for a column
0👍
I’m impressed you care about the resulting html – I only worry about getting the templates tidy and readable! But in answer to your question, have you looked at middleware?
http://docs.djangoproject.com/en/dev/topics/http/middleware/
Then use the Tidy python module to reformat the html.
- How to test a Django on_commit hook without clearing the database?
- Django-allauth HTML email not send
- Django database delete specific number of entries
- Django + Forms: Dynamic choices for ChoiceField
- How to avoid creating test database for testing in django?
0👍
I recommend to run the output of the template engine through an HTML tidier, such as µTidylib.
- Django_rest_swagger – 'staticfiles' is not a registered tag library. Must be one of:
- ValueError: Incorrect timezone setting while migrating manage.py file in Django
0👍
The readability of HTML doesn’t matter. In a system like Django or Rails, HTML is an output format, not a source format. When people edit HTML by hand, they are right to be concerned about indentation and spacing, because it is a source format, and someone will have to edit it in the future. But the output of templates is just that: output. The browser doesn’t care about the indentation. At this point the only people who read the HTML are people looking at View – Source.
0👍
If you’re using Django Templates to do string-formatting of long-but-simple strings, you can use Python’s triple-quoted strings with .format() in a function def:
def templated_string(context_dict):
return '''This is an example
of a long string across multiple lines.
It can be used to format {something} for
{name}'s own purposes, even though
it looks funny in code'''.format(**context_dict)
Then templated_string({'name': 'rileymat', 'something': 'whatever you want'})
does the sensible thing. You can generate raw HTML that way… which then has exactly the whitespace you ask for.