1👍
The problem here is that you’re not truly responding to errors. As long as the request was sent as a POST via AJAX, success
is always True
. This tells you nothing about whether the file could be parsed, or heck, even if a file was uploaded at all.
First and foremost, you code will currently always fail (500 error) because there’s no request.post
; it should be request.POST
.
Second, any time you need to pull a value out of request.POST
, you should always you dict.get
, i.e.:
filename = request.POST.get('filename')
If the field is not found in the POST data, filename
will equal None
. You should therefore, branch on this, only going on with the rest of the code if filename
is not None
, e.g.:
if filename is None:
return HttpResponseBadRequest()
else:
# the rest of your code
Third, I’m assuming filename
is an input with type="file"
. If that’s the case, it won’t actually exist in request.POST
, anyways. All file uploads go into request.FILES
. The same notes above apply:
filename = request.FILES.get('filename')
if filename is None:
return HttpResponseBadRequest()
else:
# the rest of your code
Fourth, both parse_file
and process_data
seem to be proprietary methods you created, so it’s not possible to give you much help there. But remember to judiciously use try...except
blocks whenever you’re doing anything that could fail, such as reading data from a file, or parsing that data in an assumed format that it’s not actually in.
Your parse_file
and process_data
methods should catch all possible exceptions. Then they can either propagate them upward, raise another custom exception in their place, or simply fail silently returning something like None
. It’s then up to your view here to respond accordingly, catching the exception or branching on whether the returned value is None
or not.