Hey,
I have a page with a bunch of reports/files (pdf, xlsx, etc) that can be generated & downloaded by users. For instance this mixin:
class StreamHttpResponseMixin:
""" builds and HttpResponse with streamed file/content we want to return """
def build_http_response(self, doc):
httpresponse = HttpResponse(content_type=doc["content_type"], content=doc["content"])
httpresponse['Content-Disposition'] = f'attachment; filename={doc["filename"]}'
httpresponse['Content-Transfer-Encoding'] = 'binary'
return httpresponse
Where doc
contains the xlsx files formatted by xlsxwriter. Similarly for PDF, with adjusted headers, content-types etc. Works fine, downloads the file as attachements.
The user clicks a button to obtain the report:
<button id="" type="submit" name="action" value="rapport_2" class="btn btn-secondary" data-toggle="tooltip" title="">Rapport 2</button>
Which submits via a POST (where I use the value attribute to determine what to build for a file response). THen I typically have some parameters the user can set for the different reports, which are processed as forms submitted.
That all works fine, however if I add stuff via the django.contrib.messages
framework, they don’t get displayed. Guessing because I tell the browser I want to stream stuff, and it doesn’t refresh.
I could rewrite stuff so that I actually do the same thing ajax with jquery I guess (and once I get the response, resfresh the page to display the messages as per normal). However is that a way to just tweak the mixin/headers of the HttpResponse so I don’t have to rewrite as much stuff? Maybe tweaking the headers or http content so that the browsers knows to display the messages as well?