HttpResponse works but HttpResponseRedirect does not

125 views
Skip to first unread message

Mike Dewhirst

unread,
Jan 1, 2024, 11:45:19 PM1/1/24
to Django users
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.
OpenPGP_signature.asc

Ryan Nowakowski

unread,
Jan 2, 2024, 2:21:08 PM1/2/24
to django...@googlegroups.com
https://www.rfc-editor.org/rfc/rfc9110#status.302 says:

> The server's response content usually contains a short hypertext note with a hyperlink to the different URI(s).

But this content is usually not displayed by the browser because:

> The user agent MAY (and usually does)  use the Location field value for automatic redirection.

Parenthesis added by me. So you'll need to find another way of showing a fresh form and downloading the text file.

How about including a link to the download for the filled out form on the fresh form page after redirect? The user would have to manually click the link. Or add some JavaScript to automate.

- Ryan

OSP PRO

unread,
Jan 2, 2024, 2:33:17 PM1/2/24
to django...@googlegroups.com
One potential issue in the code is that the variable fname is being used in the Content-Disposition header, but it's not defined in the provided snippet. Make sure that fname is defined and contains the desired filename for the CSV file.

Additionally, it's important to ensure that the make_csv function correctly constructs the CSV content and that it's being called with the correct data.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/B76C7735-4997-48DC-9001-6A6FB33BE8BD%40fattuba.com.

OSP PRO

unread,
Jan 2, 2024, 2:33:22 PM1/2/24
to django...@googlegroups.com
One potential issue in the code is that the variable fname is being used in the Content-Disposition header, but it's not defined in the provided snippet. Make sure that fname is defined and contains the desired filename for the CSV file.

Additionally, it's important to ensure that the make_csv function correctly constructs the CSV content and that it's being called with the correct data.

--
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...@googlegroups.com.

Alex007 MULUMBA

unread,
Jan 2, 2024, 4:38:51 PM1/2/24
to django...@googlegroups.com
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

Mike Dewhirst

unread,
Jan 2, 2024, 10:51:59 PM1/2/24
to django...@googlegroups.com
Ryan thanks for that. I was hoping there might be a Django battery. 

I don't want to create a downloadable file and offer a URL because I'd have to build infrastructure to keep everything secure. 

Currently, everything is https encrypted in both directions and nothing is saved on the server. That is a good feature.

I guess I'll study that rfc and see if I can maybe add a header without spoiling the "attachment"

M

--
(Unsigned mail from my phone)
--
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...@googlegroups.com.

Mike Dewhirst

unread,
Jan 2, 2024, 11:06:59 PM1/2/24
to django...@googlegroups.com
Merci Alex

I'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.

Cheers

Mike






--
(Unsigned mail from my phone)



-------- Original message --------
From: Alex007 MULUMBA <kandea...@gmail.com>
Date: 3/1/24 08:38 (GMT+10:00)
Subject: Re: HttpResponse works but HttpResponseRedirect does not

Reply all
Reply to author
Forward
0 new messages