Take the 2-minute tour ×

i try to upload a csv file into my web application and store it into mysql database but failed.Please can anyone help me?

my user.py script:

def import_contact(request):
if request.method == 'POST':
	form = UploadContactForm(request.POST, request.FILES)
	if form.is_valid():
		csvfile = request.FILES['file']
		print csvfile

                    testReader = csv.reader(csvfile,delimiter=' ', quotechar='|')

		for row in testReader:
			print "|".join(row)
	return HttpResponseRedirect('/admin')

	form = UploadContactForm()

vars = RequestContext(request, { 'form': form })
return render_to_response('admin/import_contact.html', vars)

my forms.py script:

class UploadContactForm(forms.Form):
file  = forms.FileField(label='File:', error_messages = {'required': 'File required'})
share|improve this question
How exactly did you fail? You haven't provided sufficient information. –  shanyu Aug 26 '09 at 2:47
i using ubuntu linux.so when i submit the csv file to upload,in terminal will print the name of the file i choosebut it didnot load into my web or save into mysql –  s_atfi Aug 26 '09 at 4:44
Did you make sure that your form element in the template has the attribute: enctype="multipart/form-data" –  Ross Light Aug 26 '09 at 4:57
yes,it is there. –  s_atfi Aug 26 '09 at 5:03
Is getcsv the function that is supposed to do the actual importing? It might help if you showed that. The code you've got here will only print. –  Daniel Roseman Aug 26 '09 at 6:38
show 1 more comment

1 Answer

Since you haven't provided the code for the getcsv function, I'll have to use my crystal ball here a bit.

One reason why the print in the for row in testReader: loop isn't working is that getcsv may already processes the file. Use the seek method to reset the objects position in the file to zero again. That way the for loop will process it properly.

Another reason why there's nothing stored in the database might be that in the code you've supplied there doesn't seem to be a reference to a model. So how should Django know what it should store and where?

share|improve this answer
add comment

Your Answer


