视图没有返回HttpResponse对象错误

时间:2014-06-06 05:15:27

标签: python mysql django django-views

我正在将excel数据导入MySQL数据库。在这样做时,我在视图中收到一个错误。

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

我在第34行收到错误;返回HttpResponseRedirect(反向(&#39; upload.views.list&#39;))&#34;:

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

我的forms.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'),
)

我不知道我在这里做错了什么。

2 个答案:

答案 0 :(得分:2)

第一行:

if request.method == "POST":

没有其他部分。执行重定向时,该方法不会发布。因此,找不到else部分,因此没有返回HttpResponse对象

答案 1 :(得分: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))