2

这是我得到的错误: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})
4

1 回答 1

4

您仅应要求返回您的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_instancerender_to_response 也可以render使用

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

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

于 2012-09-18T09:45:18.803 回答