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

我在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.报价仍在那里!

上一篇:python – 使用Django报告标头的参数


下一篇:k8gege的Ladon使用笔记