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

时间:2015-07-06 23:33:27

标签: django

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

我想传递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.

提前致谢。

1 个答案:

答案 0 :(得分:0)

您不能将发布的文件保留在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.
相关问题