在Content disposition响应文件名-python / django中添加变量 [英] Adding a variable in Content disposition response file name-python/django

查看:386
本文介绍了在Content disposition响应文件名-python / django中添加变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个变量添加到我的下面的python代码的文件名部分,以便下载的文件的名称将根据下载时的用户输入而改变。



所以,而不是Data.xlsx,它将包含我的变量(基于用户输入)+Data.xlsx。我看到一个类似的问题,但是基于PHP,不知道如何适应python。

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

提前感谢

解决方案

使用 str.format

 回复['内容-Disposition'] ='附件;文件名={}'格式(文件名)

使用 printf -style formatting

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

或连接字符串:

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


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!

解决方案

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

这篇关于在Content disposition响应文件名-python / django中添加变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆