Adding a variable in Content disposition response file name-python/django

北战南征 提交于 2019-12-11 01:45:25

问题


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!


回答1:


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 + '"'


来源:https://stackoverflow.com/questions/42380788/adding-a-variable-in-content-disposition-response-file-name-python-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!