Wednesday, April 29, 2015

upload csv file from different directories

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%}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPCf-y4Zofe-Q1YsQRqMY2qUj0bBfSTpbzOJqs%2Btyn7z4Y5XDQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment