# Trouble exporting csv-data

**URL:** <https://forum.djangoproject.com/t/trouble-exporting-csv-data/23217>\
**Category:** Forms & APIs\
**Created:** [August 20, 2023, 2:06pm UTC](https://forum.djangoproject.com/t/trouble-exporting-csv-data/23217 "2023-08-20T14:06:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![doc1977](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/doc1977/32/13416_2.png) [@doc1977](https://forum.djangoproject.com/u/doc1977)\
**Post date:** [August 20, 2023, 2:06pm UTC](https://forum.djangoproject.com/t/trouble-exporting-csv-data/23217/1 "2023-08-20T14:06:22Z")

</div>

Hi, dear DJANGOnians

I think my question today is quite simply to solve if someone knows how (as always 🙂 )

I’m quite done with my project, last issue left is to manage the capability to provide some csv-exports to the user. I started with `django-import-export` and it seems to cover my needs for this task.  
[django-import-export-documentation](https://django-import-export.readthedocs.io/en/latest/index.html)

Additionally i was searching the Forum and looking around in the internet, but I could’nt find an example to get it how to solve this.

I have this simple code part:

```auto
def export_data(request):

    response = HttpResponse(
        content_type="text/csv",
        headers={"Content-Disposition": 'attachment; filename="export.csv"'},
    )

    employees_dataset = EmployeesResource().export(profession_filters(request))
    print(employees_dataset.csv)

    return response

```

So in the end, when clicking a button and reaching this code, i get a 0Byte download, because my dataset is not in the response. the `print`-command is only for testing and in console, everything is printed as intended. so which syntax is needed to get the `employees_dataset.csv` in the `response` of the function ?

I know, this sounds some sort of joke, but i simply don’t get it.  
P.S.  
`employees_dataset.csv(response)` is NOT working 😉

---

<div class="post-metadata">

**Author:** ![antoinehumbert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/antoinehumbert/32/14851_2.png) [@antoinehumbert](https://forum.djangoproject.com/u/antoinehumbert)\
**Post date:** [August 20, 2023, 3:17pm UTC](https://forum.djangoproject.com/t/trouble-exporting-csv-data/23217/2 "2023-08-20T15:17:46Z")

</div>

If you look at the documentation of HttpResponse ([Request and response objects | Django documentation | Django](https://docs.djangoproject.com/en/4.2/ref/request-response/#django.http.HttpResponse. __init__ )) you will see that it accepts a content argument.  
So, you can define `content=employees_dataset.csv`

You may also want to work with FileResponse ([Request and response objects | Django documentation | Django](https://docs.djangoproject.com/en/4.2/ref/request-response/#fileresponse-objects)). E.g.:

```auto
import io

...
return FileResponse(io.BytesIO(employees_dataset.csv), as_attachment=True, filename="export.csv", content_type="text/csv")

```

---

<div class="post-metadata">

**Author:** ![doc1977](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/doc1977/32/13416_2.png) [@doc1977](https://forum.djangoproject.com/u/doc1977)\
**Post date:** [August 20, 2023, 3:34pm UTC](https://forum.djangoproject.com/t/trouble-exporting-csv-data/23217/3 "2023-08-20T15:34:38Z")

</div>

Thx for the tip… but if i try

```auto
response['content'] = key_dataset.csv

```

i get a  
`BadHeaderError at /employees/keys.html`

---

<div class="post-metadata">

**Author:** ![antoinehumbert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/antoinehumbert/32/14851_2.png) [@antoinehumbert](https://forum.djangoproject.com/u/antoinehumbert)\
**Post date:** [August 20, 2023, 3:36pm UTC](https://forum.djangoproject.com/t/trouble-exporting-csv-data/23217/4 "2023-08-20T15:36:14Z")

</div>

content is not an header, so you cannot use the item setter syntax.

Just use:

```auto
response = HttpResponse(content=employees_dataset.csv , content_type=..., headers=...)

```

---

<div class="post-metadata">

**Author:** ![doc1977](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/doc1977/32/13416_2.png) [@doc1977](https://forum.djangoproject.com/u/doc1977)\
**Post date:** [August 20, 2023, 3:49pm UTC](https://forum.djangoproject.com/t/trouble-exporting-csv-data/23217/5 "2023-08-20T15:49:47Z")

</div>

ok, this does make more sense … now i get exported data, but not looking like CSV. All data is in the first column of the sheet. Is this most likely a “delimiter” issue? I am in germany, here, in several cases, there is trouble with `,` versus `;` as CSV delimiter…

But THX so far for your input, problem not solved but a step further 🙂

Additionally: It seems that i have to specify codepage for download. while using `print` command, in console everything is looking fine. in Downloaded file, there are some Chars not correct. So i most likely have to specifiy “utf-8” in the process, but don’ t know where …

---

<div class="post-metadata">

**Author:** ![antoinehumbert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/antoinehumbert/32/14851_2.png) [@antoinehumbert](https://forum.djangoproject.com/u/antoinehumbert)\
**Post date:** [August 20, 2023, 4:08pm UTC](https://forum.djangoproject.com/t/trouble-exporting-csv-data/23217/6 "2023-08-20T16:08:27Z")

</div>

The default charset for http responses is utf-8 (see [Settings | Django documentation | Django](https://docs.djangoproject.com/en/4.2/ref/settings/#default-charset)). If you want to define a different charset, you can set the charset parameter in HttpResponse constructor.

For the separator problem, you may have to check id django-import-export provides options for overriding this, but that does not seem to be case case after a quick look at the documentation
