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

3

32

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.

Hughmanick answered 5/6, 2015 at 9:18 Comment(16)
Read this might help groups.google.com/forum/#!topic/django-users/m4w-RNt-DOgEncounter
unforunatly that did not help, as i am actually using quotes for values, "nothing"Hughmanick
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?Boesch
Is there a value object in your context?Adis
@mevius I am not sure and i dont think it is the breaking point, thats why i postet my template config. The template itself only loads the shown libs, [...] is only HTML-Code then i try using the filter. It got to be somewhere else, but i dont know where to look.Hughmanick
@DanielRoseman no, the value|default snippet is actually an django example, i am struggeling with the title stuff at the start, also the title is not an object its simply not passed in context.Hughmanick
If the title is not passed, how are you expecting it to work?Adis
If no title is passed it should render the string "nothing". I tested with set title in context, and it works. Can i mark comments as correct answers?Hughmanick
Check this once: #15168617Hellkite
This works {{ 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>Finned
@François i actually do not know what causes the error, if you say that unknown vars should be replaced too, the error still persits. But I dont know where i can lookup that error, as i don't have any custom filters applied nor changed the basic ones. As i said my editor marks this line as false or "incorrect". Maybe i am missing to load something?Hughmanick
"my editor marks this line as false or "incorrect"" But does it work in your browser? If you comment that line out, does that change anything?Finned
If the value is not set/not defined in the Context Django does not render the template and throws the above mentioned error.Hughmanick
If the value is Set, e.g. to None, then it returns the "nothing" text, which was defined as default.Hughmanick
That's really weird since in my case unknown_var wasn't in the context. I'd say, create a custom template tag with None as default in it: def my_default(value=None)Finned
Thanks, i think i will actually go for the custom tag. Unfortunately this only happens with my new setup, so i was wondering if there are any settings to change or control this behavior. The same code works in a different project of mine, as aspected, too.Hughmanick
S
60

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

This is correct:

{{ title|default:"nothing" }}

This throws an exception:

{{ title|default: "nothing" }}
Shope answered 8/2, 2016 at 0:48 Comment(4)
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?Nador
space was not the issue for me but i'll accept the answer, as it seems many people face that issueHughmanick
@Hughmanick If your issue was different, could you please post another answer explaining what was the problem? This might help others :)Shope
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. :(Hughmanick
R
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

Redcoat answered 11/6, 2015 at 18:12 Comment(0)
E
1

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" }}
Entrust answered 26/6, 2023 at 0:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.