How do I access Jinja2 for loop variables outside the loop?
问题 I have a Jinja2 template page which contains two separate {% for %} loops. If neither of these loops contain any items, I want the page to redirect. I'm trying to do something like this (pseudo-code): loop1 = loop.length (in first loop) loop2 = loop.length (in second loop) if loop1 + loop2 == 0: redirect # (outside both loops) Is this even possible? Is there a way to make the loop.length variables available outside their respective loops? 回答1: You can check your lists for truth, empty lists