[Python Django] Template에서 연산 사용하기 django-mathfilters
2022. 4. 11. 20:53ㆍPython/Django Framework
반응형
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 }}
반응형
'Python > Django Framework' 카테고리의 다른 글
[Python Django] Template에서 파일명에 따라 ACTIVE 처리하기, resolver_match (0) | 2022.04.06 |
---|---|
[Python Django] is_authenticated, is_anonymous, is_active (0) | 2022.04.01 |
[Python Django] 4. Decorator 이용하기 (0) | 2022.03.26 |
[Python Django] 3-8. CBV DeleteView를 이용한 게시판 글 삭제 기능 구현 (0) | 2022.03.26 |
[Python Django] 3-7. CBV UpdateView를 이용한 글 수정 기능구현 (0) | 2022.03.26 |