sharkbait sharkbait - 2 months ago 11
Python Question

If condition doesn't work in Tornado (Python)

I have a problem. In a Tornado handler I pass some parameters to my html page.

The code of this page is this:

{% if not writeable %}
{% if not usrpermdev %}
$("#chart_div").html("<div class='alert'><strong>Warning!</strong> You don't have the permission to view this page.</div>")
{% elif usrpermdev==0 %}
$("#chart_div").html("<div class='alert'><strong>Warning!</strong> You don't have the permission to view this page.</div>")
{% end %}
......


end it's like this because I want to show a
<div>
element or not in case of some permissions.

Now if I put some prints in my Tornado handler I see that for example:

usrpermdev = [{'perm': 4L}]
writeable = False


So why my code doesn't show anything after the control?

If the code enter in the if (not writeable), check if usrpermdev there is. In this case the program should not enter in both of the next if and go on.... why this is not happen?

PS. I don't see anything.... not even the warning alert! So the program don't enter in the if(s) but don't even go on...

Answer

I have the same issue just as you. But if I set

{% if writeable=="False" %}

It still does not work. It looks that tornado does not care about Boolean value in if clause. if you change writeable value to be True or False, then output it in your template

{{ writeable }}

You would see text like

True

Or

False

But it just still does not go the if clause. P.S. It's tornado 4.2.1 on Linux. So weird!


Update

Solved

I have just fixed this by using this clause {% if writeable is False %} And remember ,you should use this in your under a template block, rather than using if upper a template block.