Reorder the code, I think that when you removed 'settings.TEMPLATE_DEBUG' you changed the order of the instructions accidentally
Now works with 3 (or more) levels of inheritance skipping template, before of this change only worked with two levels. The case of a project template overwrite a application template is only a subset of the problem.