Reputation: 31548
I am making the view to list the data in my view like this
class DownloadView(ListView):
template_name = 'mytemplate.html'
Now in my that view , i want that when the page loads then i generate a pdf file in that view (which i can do) and then it should prompt the user with save file option. how can i do that
Upvotes: 0
Views: 177
Reputation: 10665
I wouldn't recommend generating a PDF during the request or using Django to serve static files.
Your best bet would be to save all your PDFs to a folder and use Apache or Nginx to serve them and set the content-type header to application/octet-stream to indicate the response is meant to be saved.
--Edit--
I believe the right solution would be to generate PDFs dynamically, but use a task queue instead of doing during the request. If you need to authenticate and authorize the user requesting a PDF, do it in Django, but use X-Accel (nginx), X-Sendfile (apache) - or an equivalent - to tell your webserver to serve the actual file.
Django is not meant to serve static files. To quote the django documentation, "just because it can, doesn't mean it should".
Generating the PDF during the request is not a good idea because it makes users wait, requests can and will timeout, long running requests tie up your server, impatient users will hit refresh (repeatedly) and finally, it's a DOS attack surface.
Upvotes: 0
Reputation: 43840
Check out the django docs, it shows populating the response['Content-Disposition']
with 'attachment; filename="somefilename.pdf"'
, but I think the following may work, if you've read the pdf file data into pdf_data
.
from django.http import HttpResponse
def serve_pdf_view(request):
pdf_data = magically_create_pdf()
return HttpResponse(pdf_data, content_type='application/pdf')
Upvotes: 3