CSV 仅获取第一条记录

发布于 2024-09-30 11:29:53 字数 1530 浏览 4 评论 0原文

我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心激荡 2024-10-07 11:29:53

取消缩进以下行:

 return HttpResponseRedirect('/clients/')

将其放入 for row in importfile: 循环内,使代码在第一次迭代后返回 HTTP 响应。

Unindent the following line:

 return HttpResponseRedirect('/clients/')

You put it inside the for row in importfile: loop, making your code return a HTTP response after the first iteration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文