I'm using this code:
<div class="stream {% if streams.online %}online{% else %}offline{% endif %}">
It seems a little redundant. I'd love to run this code
<div class="stream {{ 'online' if stream.online else 'offline' }}">
But sadly that doesn't work. Is there a shorter and less messy way of doing what I want?
How to use if statement in Django template. In a Django template, you have to close the if template tag. You can write the condition in an if template tag. Inside the block, you can write the statements or the HTML code that you want to render if the condition returns a True value.
{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.
What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.
Can't you use the yesno
built-in template filter?
As in,
<div class="stream {{ stream.online|yesno:"online,offline" }}">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With