Search code examples
pythondjangodjango-templatesfrontenddjango-template-filters

django.template.base.TemplateSyntaxError: default requires 2 arguments, 1 provided


I am trying to use django's built in 'default' filter using this code

<title>{{ title|default :"nothing" }}</title>

But it gives me the following exception

django.template.base.TemplateSyntaxError: default requires 2 arguments, 1 provided

I am using the following settings for my Template Backend

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            str(APPS_DIR.path('templates')),
        ],
        'OPTIONS': {
            'debug': DEBUG,
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'allauth.account.context_processors.account',
                'allauth.socialaccount.context_processors.socialaccount',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                'sekizai.context_processors.sekizai',
            ],
        },
    },
]

My editor marks the code as invalid, but i check like a thousand of times https://docs.djangoproject.com/en/1.8/ref/templates/builtins/

Where this is given as example:

{{ value|default:"nothing" }}

I also tried to change the name of title var, to make sure it is not a reserved keyword.


Solution

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

    This is correct:

    {{ title|default:"nothing" }}
    

    This throws an exception:

    {{ title|default: "nothing" }}