The only purpose of this project is to show an uploaded csv on the template from the template. It works. But when I restart the server or even if I just refresh the page, the uploaded csv data disappears. The request handling was not passing any data to the template via context.
My views.py is:
import csv, io
from django.shortcuts import render
from django.contrib import messages
from .models import Table
def data_upload(request):
template = "home.html"
if request.method == 'GET':
return render(request, template)
csv_file = request.FILES['file']
if not csv_file.name.endswith('.csv'):
messages.error(request, 'Please upload a .csv file.')
data_set = csv_file.read().decode('ISO-8859-1')
io_string = io.StringIO(data_set)
next(io_string)
for column in csv.reader(io_string, delimiter=','):
_, created = Table.objects.update_or_create(
page=column[0],
keyword=column[1],
interval=column[2],
email=column[3],
notes=column[4],
billing=column[5],
)
context = {
'tables': Table.objects.all()
}
return render(request, template, context)
The template upload form is:
<form method="POST" enctype="multipart/form-data" action="">{% csrf_token %}
<div class="file-field input-field">
<div class="btn">
<span>Upload a CSV FILE</span>
<input type="file" name="file">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
<button class="waves-effect waves-light btn teal" type="submit">Upload</button>
</div>
I know I need a .save() somewhere, and I've tried it but it doesn't work.
Anyone?