본문 바로가기

Django

django / template에서 1000단위(천단위) comma 표시

 

php를 쓰다가 django로 넘어오다보니 구글링이 더 늘었다. 

 

 

1. php, python 포맷 비교

 

php에서 사용하는 천단위 콤마 포맷

{{ number_format($price) }}
{{ number_format(1000) }}   // 1,000

 

python에서 사용하는 천단위 콤마 포맷

print(format(price, ","))
print(format(1000, ",")) # 1,000

 

 

 

2. django에서 효율적으로 사용하기 위한 포맷 

 

Add

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

INSTALLED_APPS = [
  .
  .
    'django.contrib.humanize',
  .
  .
]

 

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

{% load humanize %}

 

 

Example 

{% load humanize %}

{{ price|intcomma }}
{{ 1000|intcomma }}  # 1,000

 

 

파이썬 문법도 좋지만, django에서는 intcomma 사용해서 하니까 더 편하고 간결

 

 

 

 

 

참고사이트 

https://kimdoky.github.io/django/2018/05/11/django-1000comma/

반응형