2012-09-18 135 views
2

這是我收到的錯誤:The view extraio.file_uploader.views.Upload_File didn't return an HttpResponse object.「的觀點沒有返回HttpResponse對象」

有人能看到我在做什麼錯在這裏?我似乎無法弄清楚爲什麼我會得到這個異常,因爲我返回一個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}) 

回答

3

你只POST要求退回HttpResponse對象。 重寫你的觀點是這樣

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)) 

編輯:BTW你忘了設置context_instancerender_to_response 您也可以使用render代替

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

​​應該request.FILES.getlist('file')

相關問題