Django输出csv文件,文件名未设置为Content-Disposition的值 [英] Django output csv file, filename is not setting as the value of Content-Disposition

查看:71
本文介绍了Django输出csv文件,文件名未设置为Content-Disposition的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在django项目中下载具有自定义文件名的csv文件,但是以某种方式下载的文件名只是显示为"download.csv",而不是在Content-Disposition中使用filename的值.我也尝试打印出 csv_response ['Content-Disposition'] ,但是我得到了一个非常奇怪的字符串 =?utf-8?b?YXR0YWNobWVudDsgZmlsZW5hbWU9Iuivvueoi + aKpeWQjeaDheWGtV8yNDE5DI>

I want to download a csv file with custom filename in a django project, but somehow the downloaded filename just display as "download.csv" instead of using the value of filename in Content-Disposition. I also tried to print csv_response['Content-Disposition'] out, but I'm getting a very strange string =?utf-8?b?YXR0YWNobWVudDsgZmlsZW5hbWU9Iuivvueoi+aKpeWQjeaDheWGtV8yMDE5MTEyODA3NDI0Ny5jc3Yi?=

代码段是:

@action(detail=False, methods=['GET'])
def download(self, request):
    registrations = self.filter_queryset(self.get_queryset())

    csv_response = HttpResponse(content_type='text/csv')
    csv_response['Content-Disposition'] = 'attachment; filename="some_custom_name_{time}.csv"'.format(
        time=time.strftime("%Y%m%d%H%M%S", time.localtime())
    )

    writer = csv.writer(csv_response)
    writer.writerow([
        some content,
    ])

    for registration in registrations:
        term_title = '{order} th'.format(order=registration.term.order)
        course_title = registration.course.title

        writer.writerow([
            registration.user.email,
            course_title,
            term_title,
            str(registration.confirmation_code),
            str(registration.payment_due),
            str(registration.payment_paid),
            str(registration.source),
            str(registration.created_at),
            str(registration.updated_at),
            str(registration.payment_source),
        ])

    return csv_response

我正在使用的django是2.2

the django I am using is 2.2

为什么会这样?我是新手.提前谢谢

any ideas why this is happening? I am a newb. Thx in advance

chrome开发工具中的响应标头:

The response header in chrome Dev tools:

推荐答案

我通过遵循以下帖子中的答案解决了该问题: HttpResponse Django不会更改文件名

I resolved the problem, by following the answer in the below post: HttpResponse Django does not change file name

我猜想是因为需要对Content-Disposition字符串进行编码,否则,通过使用 urlquote 可以以某种方式无法对其进行操作,从而解决了该问题.关于 urlquote 的说明是此处>

I guess that it is that because the string of Content-Disposition needs to be encoded, and if no, then somehow cannot operate on that, by using urlquote, it is solved. Explanation about urlquote is here

更新:另外,无需导入 urlquote 即可解决此问题的更简单方法是添加 encode(),如下所示:

UPDATE: Also, a simpler way to resolve this without importing urlquote is to add encode(), like below:

csv_response['Content-Disposition'] = 'attachment; filename="some_custom_name_{time}.csv"'.format(
            time=time.strftime("%Y%m%d%H%M%S", time.localtime())
        ).encode()

这篇关于Django输出csv文件,文件名未设置为Content-Disposition的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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