Skip to content

Instantly share code, notes, and snippets.

@dejmail
Forked from rg3915/views.py
Created November 4, 2021 12:04
Show Gist options
  • Select an option

  • Save dejmail/8184108c3a50d7f60f9e460e7b781ee7 to your computer and use it in GitHub Desktop.

Select an option

Save dejmail/8184108c3a50d7f60f9e460e7b781ee7 to your computer and use it in GitHub Desktop.
Read csv InMemoryUploadedFile Django
import csv
import io
def import_csv(request):
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
# Read csv file InMemoryUploadedFile
file = myfile.read().decode('utf-8')
reader = csv.DictReader(io.StringIO(file))
# Generate a list comprehension
data = [line for line in reader]
save_data(data) # Not implemented
return HttpResponseRedirect(reverse('product:product_list'))
template_name = 'product_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