1

I want to build a Django webpage, where the user uploads docx files, edites them and downloads as new docx files. When the file is downloaded, the content is spoiled.

The views.py function is the following:

from docx import Document

def edit_file(request, upload_id):
    instance = get_object_or_404(DocFile, id=upload_id)
    document = Document(instance.agreement)

        # here is some code to edit the docx file

        file_name = '{}.doc'.format(contract_name)
        response = HttpResponse(document, content_type='application/msword')
        response['Content-Disposition'] = 'attachment; filename="{}"'.format(file_name)
        document.save(response)
        return response
    return render(request, 'uploads/file_detail.html', {
        'variables': variables, 'form_field': form_field
    })

I edit this kind of content file this kind of content file And get this spoiled content in the new filenew file content Is it possible to get downloaded the edited file but not the content be spoiled in this way? If no, what other ways are possible?

Mn. Hermine
  • 31
  • 1
  • 4
  • you have to explain what `Document` is and what `document.save(response)` in particular is doing. Which package are you using? And what is "spoiled"? You should pass a file in the `HttpResponse` (see for example [here](https://stackoverflow.com/questions/36392510/django-download-a-file)), I don't know if a `Document` instance is the actual file. – dirkgroten Jan 30 '19 at 16:03
  • Possible duplicate of [Django download a file](https://stackoverflow.com/questions/36392510/django-download-a-file) – dirkgroten Jan 30 '19 at 16:04
  • How is the content spoiled exactly? – Raydel Miranda Jan 30 '19 at 16:06
  • I added what is 'Document'. I use python-docx to edit and save files. – Mn. Hermine Jan 30 '19 at 16:28
  • And also added the old and new files contents – Mn. Hermine Jan 30 '19 at 16:37
  • Actually, I use your mentioned examples, and it works. Only the document content is spoiled. I change the `content_type` in `response = HttpResponse(document, content_type='application/msword')` but get the same result ((. – Mn. Hermine Jan 30 '19 at 17:45

0 Answers0