Skip to content
Advertisement

What is the equivalent of “none” in django templates?

I want to see if a field/variable is none within a Django template. What is the correct syntax for that?

This is what I currently have:

JavaScript

In the example above, what would I use to replace “null”?

Advertisement

Answer

None, False and True all are available within template tags and filters. None, False, the empty string ('', "", """""") and empty lists/tuples all evaluate to False when evaluated by if, so you can easily do

JavaScript

A hint: @fabiocerqueira is right, leave logic to models, limit templates to be the only presentation layer and calculate stuff like that in you model. An example:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement