Trouble exporting csv-data

Hi, dear DJANGOnians

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

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

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:

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 :wink:

If you look at the documentation of HttpResponse (Request and response objects | Django documentation | Django) 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). E.g.:

import io

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

Thx for the tip… but if i try

response['content'] = key_dataset.csv

i get a
BadHeaderError at /employees/keys.html

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

Just use:

response = HttpResponse(content=employees_dataset.csv , content_type=..., headers=...)
1 Like

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 :slight_smile:

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 …

The default charset for http responses is utf-8 (see Settings | Django documentation | Django). 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