1
votes

I want to pass request.FILES from a post form to a different form through HttpResponseRedirect, I tried with session variables but I cannot pass a file through them. Is it possible ? if not: How can I do it?.

The reason I want to pass an XML file is because I will need to show the file's processed data in the next view that I am redirecting to with HttpResponseRedirect. Here is a snippet:

# View that handles post data
dev my_view(request):
    if request.method == "POST":
        form = myForm(request.POST, request.FILES)
        my_file = request.FILES['xml']
        if form.is_valid():
            # Magic for passing files to the next view
            # I tried with session variables, but I couldn't pass
            # the file, as it couldn't be serialized.
            return HttpResponseRedirect(reverse('form2'))

dev my_view2(request):
    pass
    # Here I'd receive the data from the previous form
    # and handle it according to my needs.

Thanks in advance.

1
It seems like you are trying to build a form wizard?Rod Xavier
Yes, I tried to built it , but I cannot pass files through it with a custom database storage.miccke
If you are trying a form wizard, why not use django-formtools? This was once part of the django core but was removed and separated into a standalone package. django-formtools.readthedocs.org/en/latestRod Xavier
Yeah I installed them cause they don't come with django by default, but I can fit them to a custom database storage, I read that form wizards don't work good with files. Do you have any snippet ? Here is the link of the file storage that I'm using. github.com/victor-o-silva/db_file_storage. Thanks in advance.miccke

1 Answers

0
votes

You cannot keep the posted files on HTTP redirect - it's how HTTP works.

If the redirect is not an absolute necessity, you can directly call the second view function from the first one:

# View that handles post data
dev my_view(request):
    if request.method == "POST":
        form = myForm(request.POST, request.FILES)
        my_file = request.FILES['xml']
        if form.is_valid():
            # Magic for passing files to the next view
            # I tried with session variables, but I couldn't pass
            # the file, as it couldn't be serialized.
            return my_view2(request)   # <<<<<<< directly call the second view

dev my_view2(request):
    pass
    # Here I'd receive the data from the previous form
    # and handle it according to my needs.