[Answered ]-Django : can't return redirect in a sub function exception. The first function continues

1👍

You don’t return the result of b in a, so, regardless what b returns, it will still process c, and d. The return redirect will return, but it will return out of b, it does not return out of the callers of b, that doe snot make much sense.

We thus should check if b returns something, if that is the case, return the result, otherwise continue:

def a(request):
    try:
        result = b(request)
        if result is not None:
            return result
    except Exception:
        messages.error(request, 'error')
    try:
        result = c(request)
        if result is not None:
            return result
    except Exception:
        messages.error(request, 'error')
    try:
        result = d(request)
        if result is not None:
            return result
    except Exception:
        messages.error(request, 'error')
    return redirect('namespace:url_name')

Leave a comment