Hi I am writing an app that upload csv file and write csv data to models. So far everything is working fine. What i want to do now is to be able get the users to be able to upload csv files from different directories. I cannot seem to figure this out, i need directions. form the function csvimport is there are a way to get the user select the file from different directories, instead of hard coding the path (with open('/var/www/html/webapp/csvupload/data1.csv')
here is my codes.
view.py
def showuploadform(request):
if request.method=='POST':
form = uploadform(request.POST, request.FILES)
if form.is_valid():
csvimport(request.FILES['Select_CSV_Files'])
return render_to_response('csv_test.html',locals())
else:
form = uploadform()
return render(request,'uploadform.html',{'form': form})
def csvimport(request):
with open('/var/www/html/webapp/csvupload/data1.csv','rb') as csvfile:
readata=csv.reader(csvfile,delimiter=',', quotechar='"')
for row in readata:
if row[0] != 'PersonID':
data=csvfiles()
data.PersonID=row[0]
data.Firstname=row[1]
data.Lastname=row[2]
data.Address=row[3]
data.save()
return render_to_response('csv_test.html',locals())