23đź‘Ť
“not” should work, according to this example from the Django docs:
{% if not athlete_list %}
There are no athletes.
{% endif %}
You can find the example here: https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#boolean-operators
If you want to directly get the string representation of a boolean, i’m afraid you have to go what you describe as clunky. {{ variable }} puts out a string representation of the variables content or calls a function.
edit:
If you are really in need of getting the inverse value of a boolean, i would suggest to create a simple templatetag for that:
from django import template
register = template.Library()
@register.simple_tag
def negate(boolean):
return (not boolean).__str__()
put that in your_app/templatetags/templatetags.py and you can use it in your template like this:
{% negate your_variable %}
quite costly, i would stick with garnertb’s answer.
- [Django]-Why won't Django use IPython?
- [Django]-Django error: got multiple values for keyword argument
- [Django]-Annotate a queryset with the average date difference? (django)
2đź‘Ť
Another way to do it is by using the add
filter like this:
{{ variable_name|add:"-1" }}
-
True
is1
, so, it’ll become0
which isFalse
-
False
is0
, so, it’ll become-1
which isTrue
- [Django]-Altering one query parameter in a url (Django)
- [Django]-Using django-admin on windows powershell
- [Django]-Annotate a queryset with the average date difference? (django)