更改Django Pisa中的文件名

更改Django Pisa中的文件名,django,pdf,filenames,pisa,Django,Pdf,Filenames,Pisa,我想将模板呈现为pdf文件。所以我查看了这个,发现了以下代码: def write_pdf(template_src, context_dict): template = get_template(template_src) context = Context(context_dict) html = template.render(context) result = StringIO.StringIO() pdf = pisa.pisaDocument(

我想将模板呈现为pdf文件。所以我查看了这个,发现了以下代码:

def write_pdf(template_src, context_dict):
    template = get_template(template_src)
    context = Context(context_dict)
    html = template.render(context)
    result = StringIO.StringIO()
    pdf = pisa.pisaDocument(StringIO.StringIO(
        html.encode("UTF-8")), result, encoding='UTF-8')


    if not pdf.err:
        return http.HttpResponse(result.getvalue(),
                                 mimetype='application/pdf', )
    return http.HttpResponse('Gremlins ate your pdf! %s' % cgi.escape(html))
它工作得很好,但我无法更改文件名。 如果我开始下载这个文件,firefox会说“1.pdf”已经下载了。 所以我的问题是: 如何更改渲染模板的文件名? 我查过了,但没有找到答案。。(也许我太傻了^^)


非常感谢您的帮助

非常接近。您只需设置响应的
内容配置

def write_pdf(template_src, context_dict):
    template = get_template(template_src)
    context = Context(context_dict)
    html = template.render(context)
    result = StringIO.StringIO()
    pdf = pisa.pisaDocument(StringIO.StringIO(
        html.encode("UTF-8")), result, encoding='UTF-8')

    if not pdf.err:
        response = http.HttpResponse(result.getvalue(),
                                 mimetype='application/pdf')

        response['Content-Disposition'] = 'attachment; filename=whatever.pdf'

        return response
    return http.HttpResponse('Gremlins ate your pdf! %s' % cgi.escape(html))

这正是我需要的!非常好用,非常感谢!