question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HTML not getting decoded properly by Middleware in Django 2.2

See original GitHub issue

Hey! 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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
maleficecommented, May 20, 2020

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 as minify_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 to str directly without the encoding keyword argument, so no decoding takes place.

TL;DR: The middleware should handle the decoding which the PR does.

0reactions
maleficecommented, May 20, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found