Downloadable docx file in Django

前端 未结 4 745
慢半拍i
慢半拍i 2020-12-18 16:18

My django web app makes and save docx and I need to make it downloadable. I use simple render_to_response as below.

return render_to_response(\"         


        
4条回答
  •  不思量自难忘°
    2020-12-18 16:23

    Yep, a cleaner options, as stated by wardk would be, using https://python-docx.readthedocs.org/:

    from docx import Document
    from django.http import HttpResponse
    
    def download_docx(request):
        document = Document()
        document.add_heading('Document Title', 0)
    
        response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
        response['Content-Disposition'] = 'attachment; filename=download.docx'
        document.save(response)
    
        return response
    

提交回复
热议问题