This one has even stumped Chat GPT who thinks there is nothing wrong with my code. I will slowly dissect my code till I get to the bottom of what is happening but given how bizarre it is, I wanted to document it here.
I am building using Django and have a Django template. The main template calls a second template and it’s on that second template the issue is happening:
{% include "kb/select_file_dialog.html" %}
{% if 'test'=='test' %}
<script>
var dialog = document.getElementById('my_modal_2');
dialog.showModal();
</script>
This gives me the error: KeyError: "'test'=='test'"
I have no idea why it thinks it’s working with a dictionary. Without the problematic line {% if 'test'=='test' %} the code runs fine.
>Solution :
The problem is that there are no spaces between the variables and ==, as a result the template parser things this is a single identifier. Yes, Django’s template parser is not very well-implemented in my humble opinion.
You thus write:
{% if 'test' == 'test' %}
…
{% endif %}
This one has even stumped Chat GPT.
That’s not very surprising, unfortunately ChatGPT is very good in writing bogus answers.