views:

28

answers:

1

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

+3  A: 

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.

André Caron
WOW and that's why I wanted to post here, for a fresh pair of eyes. Thank you so much, thats exactly the problem.
Steve
some days indentation is NOT the easiest way to demarcate blocks ;)
KevinDTimm
@KevinDTimm: most days, too much nesting in the same function makes it hard to keep track of "nesting depth", independantly of indentation.
André Caron