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

我想通过HttpResponseRedirect将post.FILES从发布表单传递给另一个表单,我尝试了会话变量,但是我无法通过它们传递文件。 可能吗 ? 如果不是的话:我该怎么做?

我想传递一个XML文件的原因是因为我需要在下一个视图中显示文件的处理数据,我使用HttpResponseRedirect重定向到该文件。 这是一个片段:

# 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.

提前致谢。


您无法保留HTTP重定向上发布的文件 - 这就是HTTP的工作原理。

如果重定向不是绝对必要的,则可以直接从第一个视图调用第二个视图函数:

# 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/9683.html

上一篇: django: Passing posted files through HttpResponseRedirect

下一篇: Django POST request to my view from Pyres worker