How to Use If Elif Else Conditions in Django Template (HTML) - Python Web Development

Published: 30 June 2021
on channel: codevev
1,050
26

#Python #Shorts

Django doesn't only let us pass in variables from the backend code to the template but it also allows us to render certain elements on the page based on some conditions.
Just think of it as if you were able to use the if/elif/else statements inside the HTML files. And with Django, you actually can!
To do that, use the single curly braces with two percent % symbols inside them, and put your if statement in between the % signs. For example, {% if now.year == 2020 %}
And the follow it by the HTML elements that you want to display if this condition is true.
{% if now.year == 2020 %}

After that, either add an elif condition or an else statement using the same logic, and at the end "terminate" your conditional logic with {% endif %} to let Django know that you're done here.
-------------------------------------------------------------------------------------
Links:
Blog: https://evgenyurubkov.com
Twitter:   / evgenyurubkov  
Instagram:   / evgenyurubkov  
Youtube:    / @codevev  


Watch video How to Use If Elif Else Conditions in Django Template (HTML) - Python Web Development online without registration, duration hours minute second in high quality. This video was added by user codevev 30 June 2021, don't forget to share it with your friends and acquaintances, it has been viewed on our site 1,050 once and liked it 26 people.