Make Django return response as a "different filename"

Deniz Dogan picture Deniz Dogan · May 7, 2009 · Viewed 7.3k times · Source

I have a Django view which returns an HttpResponse with a special MIME type to make the user's browser "download" the file instead of view it in the browser. The problem is that the default filename that the response will be saved as is the URL which the user tried to access.

Is there any way to include a default filename in the HttpResponse object or do I have to redirect to a different URL to make this happen?

Answer

Dominic Rodger picture Dominic Rodger · May 7, 2009

There's a relevant example in the docs:

from django.http import HttpResponse

def some_view(request):
    # Create the HttpResponse object with the appropriate headers.
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
    return response