If you are rendering this HTML fragment in Django, keep in mind that all template rendering occurs in the server and not in the browser.
Your {% if ...%} block is evaluated by Django at the time the template is being rendered. By the time it has been sent to the browser, that tag has been resolved and does not exist in the rendered html.
I suggest you examine the rendered html in your browser’s developer tools to see what has been rendered and is being processed in the browser.