upload csv file from different directories

16 views
Skip to first unread message

sum abiut

unread,
Apr 29, 2015, 11:19:26 PM4/29/15
to django...@googlegroups.com
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())

forms.py

class uploadform(forms.Form):
    Select_CSV_Files=forms.FileField()

uploadform.html



<form enctype="multipart/form-data" action ="/showuploadform/" method="post">{%csrf_token%}
<table>
{{form.as_table}}
</table>
<br>
<input type="submit" name="submit" value="Upload Files" > 
</form>



{%endblock%}

sum abiut

unread,
May 2, 2015, 6:06:42 AM5/2/15
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages