简体   繁体   English

在内容处置响应文件名-python / django中添加变量

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

I am looking to add aa 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. 我想在下面的python代码的文件名部分添加一个变量,以便下载的文件名将根据用户在下载时的输入进行更改。

So instead of "Data.xlsx" it would include my variable (based on user input) + "Data.xlsx". 因此,它将包含我的变量(基于用户输入)+“ Data.xlsx”,而不是“ Data.xlsx”。 I saw a similar question but based in PHP and couldn't figure out how to adapt it to python. 我看到了一个类似的问题,但基于PHP,无法弄清楚如何使其适应python。

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

Thanks in advance! 提前致谢!

Using str.format : 使用str.format

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

Using printf -style formatting : 使用printf -style格式

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

or concatenating strings: 或串联字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM