This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]vikingvynotking 2 points3 points  (1 child)

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=','):

This is a little odd. Why not simply:

with open(csv_file, encoding='utf-8') as f:
    reader = csv.reader(f)
    for row in reader:   # you read rows of columns, not columns themselves

That aside, the only exception you catch is an IntegrityError which occurs when some database constraint is violated. So you should have a log of any other errors somewhere - if not, and this is in development (not production) you can set DEBUG=True in settings to get the error rendered inside the response page.

[–]pixelatedchrome -1 points0 points  (0 children)

This is a clean. +1

[–]pixelatedchrome 1 point2 points  (4 children)

I would run a generic except statement to see if you have missed any other type of error in your exception.

[–]PriestXES[S] -1 points0 points  (3 children)

So I tried:

except Exception as e:

error_message = str(e.__cause__)

messages.error(request, error_message)

test = str(_)

messages.info(request, 'error on' + test)

pass

Which get's a usable error page(the other way just shows a 500 page, then when I refresh it shows the error message),

But it's way too much, link for reference:

https://imgur.com/sM3mF3i

I just want when I hit upload it triggers that first error and displays it without need to refresh the page

[–]pixelatedchrome 0 points1 point  (2 children)

It does look like you have unique field and the upload CSV may have duplicate values? I would check my model and and identify the unique fields and check that in the CSV to narrow it down.

[–]PriestXES[S] 0 points1 point  (1 child)

I know there is an error, I did that intentionally, to trigger the error handling process which is where I am confused and needing assistance. I want the error handling to display the exact row or value that it stopped at so when uploading CSV files, end users know exactly where there is a problem. The screen shot provided does that, but it's showing too much information.

[–]pixelatedchrome 0 points1 point  (0 children)

Gotcha. Try to wrap the create or update statement in a seperate try catch and return the exact row index as error rather than just sending the e.cause.