I am importing the excel data to MySQL database. While doing that i am getting one error in view.
我正在将excel数据导入MySQL数据库。这样做时,我得到了一个视图中的错误。
My views.py file:
我的观点。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))
I am getting the error at line "return HttpResponseRedirect(reverse('upload.views.list'))":
我在“return HttpResponseRedirect”行(反向操作('upload.view .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
My forms.py file is:
我的形式。py文件是:
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Select a file',
help_text='max. 42 megabytes'
)
urls.py file:
url。py文件:
urlpatterns = patterns('',
url(r'^list/$', views.list, name='list'),
)
I don't know what wrong I am doing here.
我不知道我在这里做错了什么。
2 个解决方案
#1
2
You need to return a HttpResponse
object (or a subclass, like HttpResponseRedirect
) at every request. In your code, you are checking if the request is sent via POST, but you forgot to return a response otherwise. Your view should be something like:
您需要在每个请求时返回一个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
2
For the first line:
第一行:
if request.method == "POST":
there is no else part. When you perform a redirect, the method is not post. Thus, an else part cannot be found and so there is no HttpResponse object returned
没有其他的部分。当执行重定向时,该方法不是post。因此,无法找到else部分,因此不会返回HttpResponse对象
#1
2
You need to return a HttpResponse
object (or a subclass, like HttpResponseRedirect
) at every request. In your code, you are checking if the request is sent via POST, but you forgot to return a response otherwise. Your view should be something like:
您需要在每个请求时返回一个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
2
For the first line:
第一行:
if request.method == "POST":
there is no else part. When you perform a redirect, the method is not post. Thus, an else part cannot be found and so there is no HttpResponse object returned
没有其他的部分。当执行重定向时,该方法不是post。因此,无法找到else部分,因此不会返回HttpResponse对象