Reputation: 939
I have this code:
{{ object.subtotal }}
I want to print 0 in case if object.subtotal
is empty or false:
I've tried like this:
{{ object.subtotal or 0 }}
but I've got a server error 500
Upvotes: 1
Views: 77
Reputation: 476557
You can, as far as I know, not use such operators as a Django template "variable". But what you can do to replace the None
, is use the default_if_none
template filter:
{{ object.subtotal|default_if_none:0 }}
This will thus only get "triggered" in case the subtotal
is None
(not another expression with truthiness False
, this thus is semantically a bit different than the Python or
, that evaluates to the second expression in case the first expression has truthiness False
).
Furthermore this is probably better implemented at the model level, and not at the template level. Templates are used to determine how something should be rendered, not what should be rendered (semantics).
Upvotes: 2
Reputation: 47354
You can use default
filter:
{{ object.subtotal|default:0 }}
Upvotes: 5