Reputation: 3816
Was following this here:
https://docs.djangoproject.com/en/2.1/howto/outputting-pdf/
The code from there above I have in my views.py
def test_report_lab(request):
theBuffer = io.BytesIO()
p=canvas.Canvas(theBuffer)
p.drawString(100, 100, "Hello World")
p.showPage()
p.save()
return FileResponse(theBuffer, as_attachment=True, filename='hello.pdf')
And got the error:
TypeError: __init__() got an unexpected keyword argument 'as_attachment'
Not sure why, FileResponse
should be totally happy with an as_attachment?
https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.FileResponse
Upvotes: 1
Views: 1767
Reputation: 16344
This feature was added in Django 2.1. From the release notes:
Requests and Responses
[...]
The new as_attachment argument for FileResponse sets the Content-Disposition header to make the browser ask if the user wants to download the file. FileResponse also tries to set the Content-Type and Content-Length headers where appropriate.
You are likely not using the correct django version. You can check which version you are using with pip freeze
.
To make the browser treat the response as an attachment in earlier Django versions you need to manually set the Content-Disposition
header on the response object:
response = FileResponse(theBuffer, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="hello.pdf"'
return response
To avoid those kind of errors in the future make sure you have selected the correct documentation for the Django version you are using. You can select your version in the bottom right corner on every page of the docs.
Upvotes: 4