这是我得到的错误:The view extraio.file_uploader.views.Upload_File didn't return an HttpResponse object.

有人可以在这里看到我在做什么错吗?我似乎无法弄清楚为什么会因为返回HttpResponseRedirect而得到该异常。

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from models import Files
from forms import Upload_File_Form

def Upload_File(request):
    if request.method == 'POST':
        form = Upload_File_Form(request.POST, request.FILES)
        if form.is_valid():
            for f in request.FILES.get_list('file'):
                f.save()
                orgfilename = Files(orgname=f.name)
                orgfilename.save()
            return HttpResponseRedirect('success.html')
        else:
            form = Upload_File_Form()
            return render_to_response('upload.html', {'form': form})

最佳答案

您仅在HttpResponse请求中返回POST对象。
这样重写您的视图

def Upload_File(request):
    form = Upload_File_Form(request.POST or None,
                            request.FILES or None)
    if request.method == 'POST':
        if form.is_valid():
            for f in request.FILES.getlist('file'):
                f.save()
                orgfilename = Files(orgname=f.name)
                orgfilename.save()
            return HttpResponseRedirect('success.html')
   return render_to_response('upload.html', {'form': form},
                             context_instance=RequestContext(request))


编辑:顺便说一句,您忘记在您的context_instance中设置render_to_response
您也可以改用render

render(request, 'upload.html', {'form': form})


request.FILES.get_list('file')应该是request.FILES.getlist('file')

关于python - “ View 未返回HttpResponse对象”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12474344/

10-12 23:05