Button download pdf sur django
Bonjour,
je tente de creer sur mon app django un bouton download pdf
mais j obtiens ceci comme error
AttributeError at /views.download_my_pdf
'str' object has no attribute 'read'
html
views.py
qui peut m aider ? merci
je tente de creer sur mon app django un bouton download pdf
mais j obtiens ceci comme error
AttributeError at /views.download_my_pdf
'str' object has no attribute 'read'
html
<a class="btn btn-primary" href="{% url 'download_pdf' %}" role="button">Download pdf</a>
views.py
def download_pdf(request):
filename = 'faults.pdf'
content = FileWrapper(filename)
response = HttpResponse(content, content_type='application/pdf')
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = 'attachment; filename=%s' % 'faults.pdf'
return response
qui peut m aider ? merci
Configuration: Windows / Chrome 98.0.4758.102
1 réponse
-
Bonjour,
En admetatnt que la classeFileWrapper
que tu utilises soit celle-ci, il faut lui passer en paramètre le descripteur de fichier associé au fichier obtenu une fois le fichier ouvert.
filename = 'faults.pdf' with open(filename, "r") as f: content = FileWrapper(f)
Bonne chance