我正在將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'),
)
我不知道我在這裏做什麼錯。
你的反向('upload.views.list')將失敗,因爲你的url應該說url(r'^ list/$',views.list,name =' upload.views.list「) – Josh