Skip to content

Instantly share code, notes, and snippets.

@ngocnhan2003
Forked from rg3915/views.py
Created May 15, 2020 17:12
Show Gist options
  • Select an option

  • Save ngocnhan2003/adab3bae8ce225f40ff656aaf7763242 to your computer and use it in GitHub Desktop.

Select an option

Save ngocnhan2003/adab3bae8ce225f40ff656aaf7763242 to your computer and use it in GitHub Desktop.
Read InMemoryUploadedFile Django
import csv
import io
def import_csv(request):
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
# Lendo arquivo InMemoryUploadedFile
file = myfile.read().decode('utf-8')
reader = csv.DictReader(io.StringIO(file))
# Gerando uma list comprehension
data = [line for line in reader]
save_data(data)
return HttpResponseRedirect(reverse('produto:produto_list'))
template_name = 'produto_import.html'
return render(request, template_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment