[Django]-Django.template.base.TemplateSyntaxError: default requires 2 arguments, 1 provided

58👍

Make sure you don’t have a space after the colon.

This is correct:

{{ title|default:"nothing" }}

This throws an exception:

{{ title|default: "nothing" }}

3👍

Try :

{{ title|default_if_none:"nothing" }}

default_if_none will display the given string if the variable is ‘None’.

default will display the string if the variable evaluates to False, ie empty strings, empty lists etc

Also make sure you send title variable in your context , if not you must use default_if_none

0👍

With Django template filters, putting space either before or after : gets error as shown below:

             # Space (Error)
                ↓ ↓
{{ title|default : "nothing" }}

So, don’t put any space there not to get error as shown below:

          # No space (No error)
               ↓ ↓
{{ title|default:"nothing" }}

In addition, putting space either before or after | doesn’t get error:

     # Space (No error)
        ↓ ↓
{{ title | default:"nothing" }}

Leave a comment