31

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.

16
  • Read this might help groups.google.com/forum/#!topic/django-users/m4w-RNt-DOg
    – chandu
    Jun 5, 2015 at 9:24
  • 1
    unforunatly that did not help, as i am actually using quotes for values, "nothing"
    – Iwan1993
    Jun 5, 2015 at 9:33
  • I hate to ask...but are you sure that line is the culprit? Do you have any other filters or tempate tags in that template? Can you show more of your template in your question?
    – rnevius
    Jun 5, 2015 at 9:43
  • 1
    Is there a value object in your context? Jun 5, 2015 at 10:06
  • 1
    If the title is not passed, how are you expecting it to work? Jun 5, 2015 at 10:13

3 Answers 3

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" }}
4
  • 5
    Seriously in a fault tollerant system like Django, a space borks things up? Much love the Django team, I'm sure all work very hard and charge nothing for your labors, so thanks for all your hard work. But a space, really?
    – MikeyE
    Aug 3, 2018 at 12:10
  • space was not the issue for me but i'll accept the answer, as it seems many people face that issue
    – Iwan1993
    Oct 31, 2019 at 10:06
  • 1
    @Iwan1993 If your issue was different, could you please post another answer explaining what was the problem? This might help others :)
    – Thierry J.
    Oct 31, 2019 at 10:56
  • honestly, after a day i decided to restart the project - as i made up several tests from ootb and everything worked fine. Must have messed the configuration elsewhere. :(
    – Iwan1993
    Oct 31, 2019 at 13:43
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" }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.