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.
value
object in your context? – Adis{{ unknonw_var|default:"My default" }}
. So your code should work. Are you 100% sure that the code bugs on that line?<title>{{ title|default:"nothing" }}</title>
– Finnedunknown_var
wasn't in the context. I'd say, create a custom template tag withNone
as default in it:def my_default(value=None)
– Finned