Using Takashi Otaka, "Learning by Moving! Introduction to Python Django Development" I'm a beginner who started learning python and Django.
I've been studying programming for about 4 months using ruby / rails, When I changed jobs as an engineer, I decided to use python, so I started studying with this book in my hand.
Since I am a de-class amateur, I would be very grateful if you could comment on supplements and suggestions.
I want to display the division result (percentage) on the HTML of the app created by django. I also want you to round it off.
I decided to make my own filter. Below is a quote from the __old __ver. Documentation. https://djangoproject.jp/doc/ja/1.0/howto/custom-template-tags.html
The Django template system comes with a wide range of built-in tags and filters to help you solve problems with your application's presentation logic. However, the core template tag primitives alone may not meet your needs. You can extend the template engine for those cases. You can write your own tags and filters in Python and use the {% load%} tag on the template. Code placement Put your custom template tags and filters inside your Django application. If it's related to an existing application, you can bundle it with that application. If not, create a new application to hold the code. You must put the templatetags directory inside your application. Place this directory in the same hierarchy as models.py, views.py, etc. If you don't have templatetags, create one. Don't forget to put init.py in your directory and package it. Place your custom tags and filters under the templatetags directory. Name the module file the name you will use later when loading the tags. So be careful when deciding on a name so that it doesn't conflict with custom tags or filters in other applications.
Have a directory file that describes your custom templates and filters. Keep the hierarchy in the same hierarchy as models.py and views.py.
views.py
models.py
.
.
.
templatetags
├─ __init__.py
└─ hoge_tag.py
Round with the round function. value → number to be divided args → number to divide Name the filter division.
hoge_tag.py
from django import template
register = template.Library()
@register.filter(name="division")
def division(value, args):
return round(value / args * 100, 2)
The last "2" defines how many decimal places to leave after rounding. __ * However, you need to be careful about how to round numbers using the round function. __
The behavior of round () on floating point numbers may be surprising: for example, round (2.675, 2) gives 2.67 instead of 2.68 as expected. This is not a bug: This is the result of most decimals not being accurately represented by floating point numbers.
For details, refer to the following. https://docs.python.org/ja/3/library/functions.html#round
Add the following so that the calculation formula can be read in the HTML that you want to use the above calculation formula.
fuga.html
{% load hoge_tag %}
All you have to do is display it.
fuga.html
{{ hoge | division:fuga}}%
If you enter numerical values (variables are possible) in "hoge" and "fuga", you can see the rose-colored world.
-Create your own template tags and filters -Built-in functions (python official) ・ I want to process multiplication with [Django] templates → I don't have it, so I create a filter to handle it
Recommended Posts