HTML not getting decoded properly by Middleware in Django 2.2
See original GitHub issueHey!
Thanks for the great repo. I´m experiencing an issue with the MinifyHTMLMiddleware. When active, my HTML-Output looks like b’…\n\n’. So it is a bytes-literal.
I can fix it by changing
response.content = minify_html(response.content.strip())
(https://github.com/jazzband/django-pipeline/blob/master/pipeline/middleware.py#L25)
to:
response.content = minify_html(response.content.decode().strip())
Just so you know about the issue. Kind regards! 😃
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Middleware - Django documentation
This document explains all middleware components that come with Django. For information on how to use them and how to write your own...
Read more >Writing custom middleware in Django 2.2, control not coming ...
I'm trying to create custom middleware within Django 2.2 application. Below is my settings file : MIDDLEWARE = [ 'html_template.middleware.
Read more >Django Debug Toolbar - Read the Docs
7.15 2.2(2020-01-31) . ... The Debug Toolbar requires two things from core Django. ... are checked in the middleware, not the callback.
Read more >Changes — Werkzeug Documentation (2.2.x)
Fix type annotations for TypeConversionDict.get to not return an Optional ... Properly handle multi-line header folding in development server in Python 2.7.
Read more >Release Notes - Django REST framework
Note that in line with Django's policy, any parts of the framework not mentioned in the documentation should generally be considered private API,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I tested using Django 2.0.13, and it does not have this problem, but 2.1 and onward will have this problem. This is caused by the change in
strip_spaces_between_tags
(aliased asminify_html
in pipeline’s middleware) between 2.0 and 2.1.For version 2.0,
django.utils.encoding.force_text
was being used and the response bytes object is being decoded properly under the hood. For 2.1 and above,force_text
is no longer being used, and the response bytes object is being passed tostr
directly without theencoding
keyword argument, so no decoding takes place.TL;DR: The middleware should handle the decoding which the PR does.
Yeah, I just did the investigation and root cause analysis so that this can finally be resolved. I still very much prefer to have this fix upstream VS maintaining this (admittedly trivial) workaround in my projects.