[PYTHON] Write a short if-else for Django Template

Write a short if-else for Django Template

If you're using a combination of Bootstrap.css and Django's template engine

{% if param.status == 1 %}
    <!--Give the class name active for certain conditions-->
    <li class="active">
{% else %}
    <li>
{% endif %}

        <a href="#">Active</a>
    </li>
……

There are times when you have to write like this. Indentation gets confusing and It's hard to see how many lines are spent on just this much processing.

So I tried to find out how to write it short. I found a built-in filter called "yesno". https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#yesno

This filter takes a comma-separated string as an argument. If the value can be interpreted as True, from the beginning of the argument to the comma, If the value can be interpreted as False, then the first and second commas, And if the value is None, it will return from the second comma. (None is optional)

{{ value|yesno "Yes,No,None" }}

given that, When the value is True (example: "1", "hoge", etc. are OK), "Yes", When the value is False (example: "0" "" "]), it is" No ".

If you rewrite the above template with this interesting filter

<!--Give the class name active for certain conditions-->
<li class="{{ param.status|yesno:'active,' }}">
    <a href="#">Active</a>
</li>

It will be. One line will be longer, but than the previous writing The structure of the indent is refreshing and easy to see, which is a great advantage.

Recommended Posts

Write a short if-else for Django Template
Make a filter with a django template
Django # 2 (template)
Create a model for your Django schedule
Commands for creating a new django project
Build a TOP screen for Django users
Write a short property definition in Python
(For myself) Django_1 (Basic / Hello World / Template)
Django HTML Template # 2
Django 1.9 for internationalization
Django Template notes
Create a dashboard for Network devices with Django!
Django HTML template
Django Template Tips
How to write a ShellScript Bash for statement
Let's display a simple template that is ideal for Django for the first time
Web App Development Practice: Create a Shift Creation Page with Django! (Write a base template)
Write about building a Python environment for writing Qiita Qiita
Make the model a string on a Django HTML template
Django + redis Issue a dedicated URL valid for 1 hour
Build a Django environment for Win10 (with virtual space)
Django template file organization
Create a Django schedule
Start a Django project
[Memo] Build a development environment for Django + Nuxt.js with Docker
Recommendations for django, wagtail ~ Why develop a website with python ~
[Django] Create a model suitable for phone numbers / zip codes
How to write a test for processing that uses BigQuery
[Django] A collection of scripts that are convenient for development
Create a social integration API for smartphone apps with Django
[Python] Create a screen for HTTP status code 403/404/500 with Django