user2935750
user2935750

Reputation: 77

How to create a csv file in python/django via Dajaxice/Dajax?

I have a website in which I would like that people can download a csv file that is created with python.

I am using more or less the example from the Django website , but I don't manage to make it work.

This is my python code in ajax.py (I am using Dajaxice/Dajax)

def create_csv(request):

    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="results.csv"'

    writer = csv.writer(response)
    writer.writerow(['Session Nr', 'Treatment', 'Participant ID', 'Discount 1', 'Discount 2', 'Discount 3'])

    return response

This is my html/javascript where people try to download the page:

    <span onclick="download()" >DOWNLOAD</span>

    <script type="text/javascript">
       function download() {
            Dajaxice.chocolate.chocolate.create_csv(Dajax.process);
       }

    </script>  

Any hint is highly appreciated.

Upvotes: 1

Views: 119

Answers (0)

Related Questions