Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django templates, firstof or default?

Do these two commands have the same functionality in Django templates?

{{ foo|default:"bar" }}

and

{% firstof foo "bar" %}

If these work in the same way, then what are the best practices? Use firstof only if we have more than 2 arguments, or always use firstof?

like image 703
André Staltz Avatar asked Nov 28 '25 05:11

André Staltz


1 Answers

default can be considered a specialized case of firstof.

firstof outputs the first variable that is not False - Think of the or clause - it evaluates until any one of the objects evaluate to True and returns that.

It can be used to compare multiple values, whereas default is used for a specific object.

default outputs the specified default value if the object evaluates to False

like image 189
karthikr Avatar answered Nov 30 '25 21:11

karthikr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!