Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template:
- {% load humanize %}
- ...
- {% if el.filesize %}
- <a href="/get_file/{{el.filename}}">скачать ({{el.filesize|humanize_bytes:0}})</a>
- {% else %}
- (0 байт)
- {% endif %}
- templatetags/humanize.py:
- from django import template
- register = template.Library()
- @register.filter(name='humanize_bytes')
- def humanize_bytes(bytes, precision=1):
- abbrevs = (
- (1 << 40L, 'ТБ'),
- (1 << 30L, 'ГБ'),
- (1 << 20L, 'МБ'),
- (1 << 10L, 'КБ'),
- (1, 'Б')
- )
- if bytes == 0:
- return '0 Б'
- if bytes == 1:
- return '1 Б'
- for factor, suffix in abbrevs:
- if bytes >= factor:
- break
- if precision:
- return '%.*f %s' % (precision, bytes / factor, suffix)
- return '%d %s' % (int(bytes / factor), suffix)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement