Forums

If statements preventing page from showing

I have a simple bottle_app.py:

@route('/')
def bob():
    return template("bob.html")

application = default_app()

And a simple page for bob.html:

<%

status = "OK"
bob = 7

if bob == 10:
    status = "this"
"""

elif bob > 6:
    status = "that"
elif bob > 2:
    status = "the other"
"""

out = "{:.2f}".format(bob)
status_out = "{0}".format(status)

%>

<!DOCTYPE html>
<html>
<head>
  <title>Bob</title>
</head>
<body>
    <p>
        bob: {{ out }} <br />
        status: {{ status_out }}
    </p>
</body>
</html>

As it is, the result is a blank page (no error indicated, and the error log doesn't show any errors.) If I move the top triple-quote above the "if" (to comment it out), then the page displays as expected.

This is a simplification of a more complex case, in which the page displayed OK if there was just one "if" but if I followed it up with an "elif," it showed a blank page. But in this case, just having the first "if" causes the page to be blank.

Can anybody see what I'm doing wrong, here?

Thanks in advance for any help!

Is that python code in the django template file?

It doesn't work that way- ie. you can't write python code in django templates. Instead try doing that in your view and passing in the results to the template via the context.