加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

为什么python双引号在文件名中转换为连字符?

发布时间:2020-12-20 13:29:21 所属栏目:Python 来源:网络整理
导读:我在Django中使用ReportLab生成一些pdf.我跟着并试验了给 this question的答案,并意识到其中的双引号没有意义: response['Content-Disposition'] = 'inline; filename=constant_"%s_%s".pdf'% ('foo','bar') 给出文件名constant_-foo_bar-.pdf response['C
我在Django中使用ReportLab生成一些pdf.我跟着并试验了给 this question的答案,并意识到其中的双引号没有意义:

response['Content-Disposition'] = 'inline; filename=constant_"%s_%s".pdf'
% ('foo','bar')

给出文件名constant_-foo_bar-.pdf

response['Content-Disposition'] = 'inline; filename=constant_%s_%s.pdf' 
% ('foo','bar')

给出文件名constant_foo_bar.pdf

为什么是这样?它只是与文件系统的slug-esque sanitisation有关吗?

解决方法

从 this question的研究看来,它实际上是浏览器进行编码/转义.我用 cURL来确认Django本身没有逃脱这些标题.首先,我设置了一个最小的测试视图:

# views.py 
def index(request):
    response = render(request,'template.html')
    response['Content-Disposition'] = 'inline; filename=constant"a_b".html'
    return response

然后跑了:

carl@chaffinch:~$HEAD http://localhost:8003
200 OK
Date: Thu,16 Aug 2012 19:28:54 GMT
Server: WSGIServer/0.1 Python/2.7.3
Vary: Cookie
Content-Type: text/html; charset=utf-8
Client-Date: Thu,16 Aug 2012 19:28:54 GMT
Client-Peer: 127.0.0.1:8003
Client-Response-Num: 1
Content-Disposition: inline; filename=constant"a_b".html

查看标题:filename =常量“a_b”.html.报价仍在那里!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读