2014-06-06 51 views
0

我正在將excel數據導入到MySQL數據庫。雖然這樣做我得到一個錯誤。視圖沒有返回HttpResponse對象錯誤

我views.py文件:

db = MySQLdb.connect(host="localhost", 
        user="root", 
        passwd="", 
        db="upload") 
test = 'c' 

def list(request): 
    if request.method == 'POST': 
     form = DocumentForm(request.POST, request.FILES) 
     if form.is_valid(): 
      newdoc = Document(docfile = request.FILES['docfile']) 
      newdoc.save() 
      newdoc = newdoc.docfile.name 
      newdoc = str(newdoc) 
      wb = xlrd.open_workbook(newdoc) 
      sh = wb.sheet_by_index(0) 
      c = 1 
      while c < len(sh.col(0)): 
       first = sh.col_values(0)[c] 
       second = sh.col_values(1)[c] 
       x = db.cursor() 
       db.set_character_set('utf8') 
       x.execute('SET NAMES utf8;') 
       x.execute('SET CHARACTER SET utf8;') 
       x.execute('SET character_set_connection=utf8;') 
       x.execute("INSERT INTO testcont_content(title, description) VALUES('%s','%s');"%(first,second)) 
       db.commit() 
       c=c+1 
      return HttpResponseRedirect(reverse('upload.views.list')) 
     else: 
      form = DocumentForm() # A empty, unbound form 
     documents = Document.objects.all() 
     return render_to_response('upload/list.html',{'documents': documents, 'form': form, 'test': test,},context_instance=RequestContext(request)) 

我得到了錯誤的路線 「返回HttpResponseRedirect(逆轉( 'upload.views.list'))」:

Internal Server Error: /upload/list/ 
Traceback (most recent call last): 
    File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 133, in get_response 
    raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name)) 
ValueError: The view upload.views.list didn't return an HttpResponse object. 
[06/Jun/2014 10:39:20] "GET /upload/list/ HTTP/1.1" 500 60044 

我的形式.py文件是:

class DocumentForm(forms.Form): 
    docfile = forms.FileField(
     label='Select a file', 
     help_text='max. 42 megabytes' 
    ) 

urls.py文件:

urlpatterns = patterns('', 
    url(r'^list/$', views.list, name='list'), 
) 

我不知道我在這裏做什麼錯。

+0

你的反向('upload.views.list')將失敗,因爲你的url應該說url(r'^ list/$',views.list,name =' upload.views.list「) – Josh

回答

2

您需要在每個請求時返回一個HttpResponse對象(或子類,如HttpResponseRedirect)。在你的代碼中,你正在檢查請求是否通過POST發送,但是你忘記了返回一個響應。你的看法應該是這樣的:

def list(request): 
    if request.method == 'POST': 
     form = DocumentForm(request.POST, request.FILES) 
     if form.is_valid(): 
      newdoc = Document(docfile = request.FILES['docfile']) 
      # ... Your process 
      return HttpResponseRedirect(reverse('upload.views.list')) 
    else: 
     form = DocumentForm() # A empty, unbound form 

    # We reach there when the HttpResponseRedirect has not been called 
    documents = Document.objects.all() 
    return render_to_response('upload/list.html', 
           {'documents': documents, 'form': form, 'test': test,}, 
           context_instance=RequestContext(request)) 
2

對於第一行:

if request.method == "POST": 

沒有else部分。當您執行重定向時,該方法不會發布。因此,找不到一個else部分,因此沒有HttpResponse對象返回

相關問題