Merci AlexI'll have to try what you suggest - not with pandas because the csv 
part is working well - but perhaps assembling the response as a separate object 
and then trying to redirect back to the same URL. Might have to look at the 
Django source and try to comprehend what it is doing.CheersMike--(Unsigned mail 
from my phone)
-------- Original message --------From: Alex007 MULUMBA 
<kandealex...@gmail.com> Date: 3/1/24  08:38  (GMT+10:00) To: 
django-users@googlegroups.com Subject: Re: HttpResponse works but 
HttpResponseRedirect does not 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 responseLe mar. 2 janv. 2024 à 05:45, Mike Dewhirst 
<mi...@dewhirst.com.au> 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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3b59be97-0cf9-4654-97ac-86da72ed4737%40dewhirst.com.au.




-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CANzESc2Pqq6r9Z06RKy%2BY-LBOwn5KdTQtkvuWVe3pCryrKe%2Bzg%40mail.gmail.com.

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/6594dd40.050a0220.6d9a5.5fa5SMTPIN_ADDED_MISSING%40gmr-mx.google.com.

Reply via email to