GregarityNow
GregarityNow

Reputation: 738

Flask Compare strings

If I upload a file to my flask script and I don't like it, I set filename to "None" so it shows a default image. Then, in my Flask template, I want to test to see if filename is "None"; if so, show the default. Else, show the file. Here is my template code, which does not work:

<!doctype html>
<title>Hello from Flask</title>
{% if {{filename}} is "None" %}:
  <h1>some text<img src="{{filename}}"> more text!</h1>
{% else %}
  <h1>Impossible file: here is tha default so<img src="MB.png"> boi</h1>
{% endif %}

I think the problem lies in the string compare. I cannot figure out how to properly compare strings, I think.

Thanks!

Upvotes: 2

Views: 4049

Answers (2)

davidism
davidism

Reputation: 127200

Set filename to None (or the empty string if you must use a string for some reason), not the string "None". Test the variable or use is not none.

{% if filename %}
{% if filename is not none %}

Upvotes: 3

JulienD
JulienD

Reputation: 7293

What about this?

{% if filename == "None" %}

You don't need double brackets inside {% %}

Ex: https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#operator

Upvotes: 6

Related Questions