我一直在处理我的CSV上传一段时间了,我终于让它工作了(有点哈哈)就像现在一样,我的代码只会从CSV文件中提取第一条记录,而且我有一直在看它太久了,我确定我错过了什么。这是我的views.py
@login_required
def importClient(request):
print "its being called"
if request.FILES:
form = ImportClientForm(request.POST, request.FILES)
if form.is_valid():
print "its valid!!"
if '.csv' in request.FILES['contact_file'].name:
print "It's a CSV file!!!"
importfile = csv.DictReader(request.FILES['contact_file'])
for row in importfile:
#establish client name
cn = row.get('Customer', None)
c = Clients(
client_name = cn,
phone = "",
phone_cell = "",
fax = "",
email = "",
add_1 = "",
add_2 = "",
city = "",
province = "",
country = "",
postal = "",
)
#check to see if client exists already
already_there = Clients.objects.filter(client_name = cn)[:1]
if not already_there:
c.save()
return HttpResponseRedirect('/clients/')
else:
form = ImportClientForm()
return render_to_response('clients/importClients.html', {
'form': form}, context_instance=RequestContext(request))
是否有我遗失的东西,我确信它非常简单。
谢谢, 史蒂夫
答案 0 :(得分:3)
取消以下一行:
return HttpResponseRedirect('/clients/')
将它放在for row in importfile:
循环中,使代码在第一次迭代后返回HTTP响应。