django 템플릿에서 기본 연산
4390 단어 djangodjango 개발 흔한 구덩이
{{ value|add:10}} value=5, 15
Django 모델 빼기:
{{value|add:-10}} value=5, -5, ,
Django 모듈 곱셈:
{% widthratio 5 1 100 %} :5/1 *100, 500,widthratio , 1/ 2* 3, , 2=1
Django는 나눗셈을 하는데, 여기에widthratio라는 방법을 사용합니다.
<td>{% widthratio foo.product_amount 100 1 %}td>#}
widthratio ,
{% widthratio this_value max_value max_width as width %}
{% blocktrans %}The width is: {{ width }}{% endblocktrans %}
<td>{{ foo.product_amount |floatformat:5 }}td>
register = template.Library()
add filter , :
A^2: {% widthratio A 1 A %}
(A+B)^2: {% widthratio A|add:B 1 A|add:B %}
(A+B) * (C+D): {% widthratio A|add:B 1 C|add:D %}
templatehelper.py
@register.filter
def div(value, div):
'''
,
:param value:
:param div:
:return:
'''
return round((value / div), 2)
, {% load templatehelper %}:
{{ foo.product_amount |div:100 }}
어리석은 방법을 시도해 보았지만 효력이 발생하지 않았고 효력이 발생하더라도 소수점 뒤에 있는 값을 무시하는 경우가 발생하기 때문에 권장하지 않습니다.
<td>{% widthratio foo.product_amount 100 1 as width %}{% blocktrans %}{{ width }}{% endblocktrans %}td>#}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Django의 질문 및 답변 웹사이트환영 친구, 이것은 우리의 새로운 블로그입니다. 이 블로그에서는 , 과 같은 Question-n-Answer 웹사이트를 만들고 있습니다. 이 웹사이트는 회원가입 및 로그인이 가능합니다. 로그인 후 사용자는 사용자의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.