import pandas as pd from django.http import HttpResponse # Assuming Django
context data_frame = pd.DataFrame.from_records(data) data_frame.to_csv(
"output.csv", index=False) # Save as CSV with open("output.csv", 'rb') as
f: response = HttpResponse(f.read(), content_type='text/csv') response[
'Content-Disposition'] = 'attachment; filename="output.csv"' return response

Le mar. 2 janv. 2024 à 05:45, Mike Dewhirst <[email protected]> a
écrit :

> I wonder if someone can point out my mistake?
>
> The following code happily downloads a constructed text file ...
>
>     csv = make_csv(context['result'])
>
>     # This downloads a csv file to the user desktop
>
>     return HttpResponse(
>
>         csv,
>
>         headers={
>
>             'Content-Type': 'text/plain',
>
>             # Set the content-disposition header to prompt user download
>
>             'Content-Disposition': f'attachment; filename="{fname}.csv"'
>
>         },
>
>     )
>
>
> ... but leaves all the entered (unbound) form data on display. However, I
> want the form cleared ready for the next set of user data.
>
> The following code clears the entered data but fails to download the
> constructed file.
>
>     csv = make_csv(context['result'])
>
>     # This fails to download a csv file
>
>     return HttpResponseRedirect(
>
>         "",
>
>         csv,
>
>         headers={
>
>             'Content-Type': 'text/plain',
>
>             # Set the content-disposition header to prompt user download
>
>             'Content-Disposition': f'attachment; filename="{fname}.csv"'
>
>         },
>
>     )
>
>
> Many thanks for any help
>
> Cheers
>
> Mike
>
> --
>
> Signed email is an absolute defence against phishing. This email has
> been signed with my private key. If you import my public key you can
> automatically decrypt my signature and be sure it came from me. Your
> email software can handle signing.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/3b59be97-0cf9-4654-97ac-86da72ed4737%40dewhirst.com.au
> <https://groups.google.com/d/msgid/django-users/3b59be97-0cf9-4654-97ac-86da72ed4737%40dewhirst.com.au?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CANzESc2Pqq6r9Z06RKy%2BY-LBOwn5KdTQtkvuWVe3pCryrKe%2Bzg%40mail.gmail.com.

Reply via email to