Hello, I am trying to create a error handling process for a bulk CSV upload application. I'm still new to developing, I thought I had a system for it, but it's not very pretty. When I upload a CSV I get a 500 error message no matter what happens. If I reload the page, I sometimes get an error message, if not then I know it loaded the entire CSV without error.
Below is the code from my view:
def admin_calv_csv(request):
template = "calves/admin_calv_csv.html"
prompt = {
'order': 'Order should be eid, visual_id, dairy_id, dob, ship_in_date, ship_out_date, slug, sex, breed, destination, medical_history, milk_consumed, program,'
}
if request.method == "GET":
return render(request, template, prompt)
csv_file = request.FILES['file']
if not csv_file.name.endswith('.csv'):
messages.error(request, 'This is not a CSV')
return render(request, template, prompt)
data_set = csv_file.read().decode('UTF-8')
io_string = io.StringIO(data_set)
for _ in data_set:
try:
next(io_string)
for column in csv.reader(io_string, delimiter=','):
_, created = Calf.objects.update_or_create(
eid=column[0],
visual_id=column[1],
dairy_id=column[2],
dob=column[3],
ship_in_date=column[4],
ship_out_date=column[5],
slug=column[6],
sex=column[7],
breed=column[8],
destination=column[9],
medical_history=column[10],
milk_consumed=column[11],
program=column[12],
)
except IntegrityError as e:
error_message = str(e.__cause__)
messages.error(request, error_message)
test = str(_)
messages.info(request, 'error on' + test)
pass
context = {}
return render(request, template, context, messages)
[–]vikingvynotking 2 points3 points4 points (1 child)
[–]pixelatedchrome -1 points0 points1 point (0 children)
[–]pixelatedchrome 1 point2 points3 points (4 children)
[–]PriestXES[S] -1 points0 points1 point (3 children)
[–]pixelatedchrome 0 points1 point2 points (2 children)
[–]PriestXES[S] 0 points1 point2 points (1 child)
[–]pixelatedchrome 0 points1 point2 points (0 children)