django: Passing posted files through HttpResponseRedirect

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.


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.
链接地址: http://www.djcxy.com/p/9684.html

上一篇: 端口和套接字有什么区别?

下一篇: django:通过HttpResponseRedirect传递发布的文件