Auto download result from custom script as file

22 views
Skip to first unread message

Mats Brorson

unread,
May 14, 2021, 6:57:03 AMMay 14
to NetBox
Allright, next challenge:-)

I have a script that gather some information from various models that the user would like to download as a file (in this case CSV)

Now returning my data ends up in the results part when script is done. 

Next I tried to use Django HttpResponse to automatically stream it as a file to my client, just as done when exporting a model from the UI. Code snippet I tried, just for test purpouse:

        response = HttpResponse(
            content_type='text/csv'
        )
        response["Content-Disposition"] = "inline;filename='output.csv'"
        writer = csv.writer(response)
        writer.writerow(['object1', 'A', 'B', 'C'])
        writer.writerow(['object2 ', 'A', 'B', 'C',])

I does not stream, but under the output tab I get:
<HttpResponse status_code=200, "text/csv">

Is it possible to achieve what I want, or am I doing it wrong?
Reply all
Reply to author
Forward
0 new messages