Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django Template Error: KeyError: "'test'=='test'" (string == string)

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'"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading