Skip to content

Instantly share code, notes, and snippets.

@simo97
Forked from imkevinxu/template.html
Created February 27, 2018 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simo97/c3997085d1d44f9e651f64e445209995 to your computer and use it in GitHub Desktop.
Save simo97/c3997085d1d44f9e651f64e445209995 to your computer and use it in GitHub Desktop.
Django code to read uploaded CSV file
<form action="{% url %}" method="post" enctype="multipart/form-data">{% csrf_token %}
<input type="file" name="csv_file" />
<input type="submit" value="Upload" />
</form>
def index(request):
if request.POST and request.FILES:
csvfile = request.FILES['csv_file']
dialect = csv.Sniffer().sniff(codecs.EncodedFile(csvfile, "utf-8").read(1024))
csvfile.open()
reader = csv.reader(codecs.EncodedFile(csvfile, "utf-8"), delimiter=',', dialect=dialect)
return render(request, "index.html", locals())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment