본문 바로가기

Django

django / template 연산 사용법 django-mathfilters

 

Install

terminal 창에 입력, django-mathfilters를 설치


pip install django-mathfilters

 

Add

1. Settings.py 파일에 'mathfilters' 추가

INSTALLED_APPS = [
  .
  .
    'mathfilters',
  .
  .
]

 

2. 사용할 template에 {% load mathfilters %} 추가

{% load mathfilters %}

 

Usage

  • sub – subtraction
  • mul – multiplication
  • div – division
  • intdiv – integer (floor) division
  • abs – absolute value
  • mod – modulo
  • addition – replacement for the add filter with support for float / decimal type

 

 

Example 

ex) a|연산자:b

{% load mathfilters %} 


<h1>Basic math filters</h1>

<ul>
    <li>8 + 3 = {{ 8|add:3 }}</li>

    <li>13 - 17 = {{ 13|sub:17 }}</li>

    {% with answer=42 %}
    <li>42 * 0.5 = {{ answer|mul:0.5 }}</li>
    {% endwith %}

    {% with numerator=12 denominator=3 %}
    <li>12 / 3 = {{ numerator|div:denominator }}</li>
    {% endwith %}

    <li>|-13| = {{ -13|abs }}</li>
</ul>

 

더하기(add)는 많이 예제가 있어서 쉬웠는데, 나누기는 아무리 찾아도 어려웠다.

divide? division? 찾다가 결국 좋은 예제 발견. 

 

해당 면적을 평수로 나타내기 위해 필요했다.

ex. {{ use_area|div:3.3 }}

 

 

 

 

참고사이트 

https://pypi.org/project/django-mathfilters/

반응형