[Fixed]-How can I use a JSON response in conditional python logic?

1👍

There are two ways to solve this. If I were you, I would just leave the variable in text format and in your code say:

{% if JSON_BOOL == "True" %}
...
{% else %}
...the rest of your code

Otherwise you could cast it to a boolean on the python side of things. This would look as follows in your view:

if JSON_BOOL == "True":
    JSON_BOOL = True
else:
    JSON_BOOL = False

I’m not aware of a way to cast variables to different types within the template itself..this seems to be outside of its scope of functionality, and either way it’s better to keep functionality out of templates. Hope this helps.

👤JwM

Leave a comment