Change HttpResponse in CBV

Hi

I want to change my function-based view to a class-based view.
But I haven’t read anywhere how to customize the html_code within a class-based view, like a method to change the HttpResponse of the class-based view.

Thats the important part of my function-based view:

html_not_nice = loader.get_template(template).render(context, request)

… Do some stuff to make html nice … (like deleting lines with no content, because of template tags and optimize indentations)

return HttpResponse(html_nice, content_type=‘text/html’)

Thanks for hints.

First a side note: I hope you realize that the majority of the time, this is all wasted effort. In 90+% of the cases, there’s no material benefit to doing this.

Also, there are already packages available to “minify” html being sent back to browsers. If this is something that you really want to do, you don’t want to do it in the view (you would need to do it with every view). Instead, you want to use some middleware to do it across all your views.

See django-minify-html · PyPI as one such project.

Now, if you feel like you really need to have control over this, you’ll probably end up overriding the render_to_response method of your CBV.