|
11 | 11 | from django.core.exceptions import ObjectDoesNotExist, PermissionDenied |
12 | 12 | from django.db import transaction |
13 | 13 | from django.db.models import Q |
14 | | -from django.http import Http404, HttpResponse, HttpResponseRedirect |
| 14 | +from django.http import FileResponse, Http404, HttpResponse, HttpResponseRedirect |
15 | 15 | from django.shortcuts import get_object_or_404, render |
16 | 16 | from django.template.loader import get_template |
17 | 17 | from django.urls import reverse |
@@ -1915,10 +1915,11 @@ def render_as_docx(self, context, template, filename): |
1915 | 1915 | new_parser = HtmlToDocx() |
1916 | 1916 | new_parser.add_html_to_document(html, document) |
1917 | 1917 | document.save(buf) |
| 1918 | + buf.seek(0) |
1918 | 1919 |
|
1919 | | - response = HttpResponse(buf.getvalue(), content_type="application/docx") |
1920 | | - response["Content-Disposition"] = f"attachment; filename={filename}" |
1921 | | - return response |
| 1920 | + return FileResponse( |
| 1921 | + buf, as_attachment=True, filename=filename, content_type="application/docx" |
| 1922 | + ) |
1922 | 1923 |
|
1923 | 1924 | def get_slugified_file_name(self, export_type): |
1924 | 1925 | return f"{datetime.date.today().strftime('%Y%m%d')}-{slugify(self.object.title)}.{export_type}" |
@@ -1983,10 +1984,11 @@ def render_as_docx(self, context, template, filename): |
1983 | 1984 | new_parser = HtmlToDocx() |
1984 | 1985 | new_parser.add_html_to_document(html, document) |
1985 | 1986 | document.save(buf) |
| 1987 | + buf.seek(0) |
1986 | 1988 |
|
1987 | | - response = HttpResponse(buf.getvalue(), content_type="application/docx") |
1988 | | - response["Content-Disposition"] = f"attachment; filename={filename}" |
1989 | | - return response |
| 1989 | + return FileResponse( |
| 1990 | + buf, as_attachment=True, filename=filename, content_type="application/docx" |
| 1991 | + ) |
1990 | 1992 |
|
1991 | 1993 | def get_slugified_file_name(self, export_type): |
1992 | 1994 | return f"{datetime.date.today().strftime('%Y%m%d')}-{slugify(self.object.title)}.{export_type}" |
|
0 commit comments