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
- [Django]-UUID as default value in Django model
- [Django]-What is the equivalent of django.db.models.loading.get_model() in Django 1.9?
- [Django]-How to migrate back from initial migration in Django 1.7?
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" }}
- [Django]-Django not sending emails to admins
- [Django]-Django error: render_to_response() got an unexpected keyword argument 'context_instance'
- [Django]-Create Custom Error Messages with Model Forms
Source:stackexchange.com