6

I am looking to add a a variable into the file name section of my below python code so that the downloaded file's name will change based on a user's input upon download.

So instead of "Data.xlsx" it would include my variable (based on user input) + "Data.xlsx". I saw a similar question but based in PHP and couldn't figure out how to adapt it to python.

response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename= "Data.xlsx"'

Thanks in advance!

falsetru
  • 357,413
  • 63
  • 732
  • 636
gluc7
  • 535
  • 1
  • 6
  • 19

1 Answers1

13

Using str.format:

response['Content-Disposition'] = 'attachment; filename= "{}"'.format(filename)

Using printf-style formatting:

response['Content-Disposition'] = 'attachment; filename= "%s"' % filename

or concatenating strings:

response['Content-Disposition'] = 'attachment; filename= "' + filename + '"'
falsetru
  • 357,413
  • 63
  • 732
  • 636