[PYTHON] I want to use complicated four arithmetic operations in the IF statement of the Django template! → Use a custom template

Purpose

For Django, use ** built-in tags and filters ** if you want dynamic processing when using template files. For example, IF and For statements, and also extends and blocks. See below for details.

Reference: Built-in Tags and Filters

What I want to do this time is modulo ** in the ** if tag in the template. Below is a sample code.

        {% for user in users%}
            {%if the remainder is 1%}
                <div class="columns is-mobile">
            {% endif %}
            <div class="column">
                <div>
                    ~abridgement~
                </div>
            </div>
            {%if when the remainder is 0%}
                </div>
            {% endif %}
        {% endfor %}

There is a tag that is already provided that returns True if it is divisible like ** divisible by **, but when the remainder is 1, this process, when the remainder is 2, this process ... Can not.

In such a case, you can solve it by preparing ** Customer Template **.

How to do a custom template

First, create the following directories and files under the project.

project/
 ├ templatetags/
           └ tags.py

And add the following to the `INSTALLED_APPS` of the `` `settings.py``` file

settings.py


INSTALLED_APPS = [
    'project',
]

Edit tags.py as follows

settings.py


from django import template

register = template.Library()

@register.filter
def modulo(num, val):
    return num % val

Finally, load the tag with the template you want to use the custom template. {% load templates%} is important.

{% load tags %}

~abridgement~

        {% for user in users%}
            {% if forloop.counter|modulo:3 == 1 %}
                <div class="columns is-mobile">
            {% endif %}
            <div class="column">
                <div>
                    ~abridgement~
                </div>
            </div>
            {% if forloop.counter|divisibleby:"3" %}
                </div>
            {% endif %}
        {% endfor %}

Have a nice Django life, everyone!

Recommended Posts

I want to use complicated four arithmetic operations in the IF statement of the Django template! → Use a custom template
I want to sort a list in the order of other lists
I want to use Python in the environment of pyenv + pipenv on Windows 10
If you want to display values using choices in a template in a Django model
How to count the number of elements in Django and output to a template
I want to set a life cycle in the task definition of ECS
I want to see a list of WebDAV files in the Requests module
I want to use the R dataset in python
I want to use a network defined by myself in PPO2 of Stable Baselines
I want to use only the normalization process of SudachiPy
[Introduction to Python] How to use the in operator in a for statement?
I want to know the population of each country in the world.
[Django] What to do if the model you want to create has a large number of fields
[Django] Carefully explain the escape route if you really want to use the table of another application
I want to print in a comprehension
If you want a singleton in python, think of the module as a singleton
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to color a part of an Excel string in Python
I want to use the Qore SDK to predict the success of NBA players
I want to leave an arbitrary command in the command history of Shell
I made a program to check the size of a file in Python
I tried to display the altitude value of DTM in a graph
Python: I want to measure the processing time of a function neatly
A note that you want to manually decorate the parameters passed in the Django template form item by item
If you want to switch the execution user in the middle of a Fabric task, settings context manager
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
I want to create an API that returns a model with a recursive relationship in the Django REST Framework
I want to pin Datetime.now in Django tests
The story of IPv6 address that I want to keep at a minimum
I created a script to check if English is entered in the specified position of the JSON file in Python.
I want to create a window in Python
I failed to install django with pip, so a reminder of the solution
I made an appdo command to execute a command in the context of the app
I want to use PyTorch to generate something like the lyrics of Japari Park
I want to add silence to the beginning of a wav file for 1 second
I want to store the result of% time, %% time, etc. in an object (variable)
I thought it would be slow to use a for statement in NumPy, but that wasn't the case.
I want to customize the appearance of zabbix
I want to use the activation function Mish
I want to display the progress in Python!
I want to upload a Django app to heroku
[Python] I want to use only index when looping a list with a for statement
I want to use a python data source in Re: Dash to get query results
I want to take a screenshot of the site on Docker using any font
I want to replace the variables in the python template file and mass-produce it in another file.
I want to create a lunch database [EP1] Django study for the first time
I want to create a lunch database [EP1-4] Django study for the first time
How to check in Python if one of the elements of a list is in another list
I want to use Linux commands at the command prompt! Use Linux commands at the command prompt instead of Git Bash
I want to embed a variable in a Python string
I want to transition with a button in flask
I want to grep the execution result of strace
I want to scroll the Django shift table, but ...
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to fully understand the basics of Bokeh
I want to write in Python! (3) Utilize the mock
Convenient to use matplotlib subplots in a for statement
When you want to plt.save in a for statement
I want to increase the security of ssh connections