CSV 仅获取第一条记录
我已经在 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))
是否有我遗漏的东西,我确信它真的很简单。
谢谢, 史蒂夫
I have been working on my CSV upload for a little while now, and I finally got it working (sort of haha) As it stands right now, my code will only pull the first record from the CSV file, and I have been looking at it too long, and Im sure I am missing something. Here is my 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))
Is there something that I am missing, I am sure its really simple.
Thanks,
Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
取消缩进以下行:
将其放入
for row in importfile:
循环内,使代码在第一次迭代后返回 HTTP 响应。Unindent the following line:
You put it inside the
for row in importfile:
loop, making your code return a HTTP response after the first iteration.