It's difficult to know which if endif belongs to if they are on the same level. It might be a good idea to either indent nested ones or at a {# #} django comment to endif describing which one it closes.