Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django输出csv文件,文件名未设置为内容处置的值_Django - Fatal编程技术网

Django输出csv文件,文件名未设置为内容处置的值

Django输出csv文件,文件名未设置为内容处置的值,django,Django,我想在django项目中下载一个带有自定义文件名的csv文件,但不知何故,下载的文件名只是显示为“download.csv”,而不是在内容配置中使用文件名的值。我还试图打印出csv_response['Content-Disposition'],但我得到了一个非常奇怪的字符串=?utf-8?b?YXR0YWNOBWVUDSZMLSZW5HBWU9IUIVVUEOI+aKpeWQjeaDheWGtV8yMDE5MTEyODA3NDI0Ny5jc3Yi?= 代码片段是: @action(detail

我想在django项目中下载一个带有自定义文件名的csv文件,但不知何故,下载的文件名只是显示为“download.csv”,而不是在内容配置中使用文件名的值。我还试图打印出
csv_response['Content-Disposition']
,但我得到了一个非常奇怪的字符串
=?utf-8?b?YXR0YWNOBWVUDSZMLSZW5HBWU9IUIVVUEOI+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

知道为什么会这样吗?我是新手。 提前Thx

chrome开发工具中的响应标题: 更改为:

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

我通过以下帖子中的答案解决了问题:

我猜这是因为内容处置字符串需要编码,如果没有,那么不知何故无法对其进行操作,通过使用
urlquote
,问题就解决了。 有关
urlquote
的说明如下

更新: 另外,在不导入
urlquote
的情况下解决此问题的更简单方法是添加
encode()
,如下所示:

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

您是否检查了您的客户端(例如,在chrome中使用)是否正确设置了所有预期的标题?@mfrackowiak下载文件后,我发现响应标题中没有包含内容配置,但我不明白原因。该文件没有问题。我认为您不需要在文件名周围加引号,我建议您删除该文件,然后重试…'附件filename=some_custom_name_{time}.csv'@prime_hit no,这不是原因,我以前试过。这是编码问题。我在发帖之前已经试过了,它不起作用。下载文件后,我还发现响应头中没有包含内容配置。您是否尝试使用一些普通字符串,如“Abc.csv”?您是否可以检查传递硬编码值是否解决了问题。@NalinDobhal如果不使用
urlquote
,则它不起作用。