- Author:
- bryanhelmig
- Posted:
- January 14, 2010
- Language:
- Python
- Version:
- 1.1
- Score:
- 0 (after 2 ratings)
This template tag is dead simple, if you have a file that is 45.3 kb, don't display it as 46,387 bits, display it as 45.3 kb!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | from django import template
register = template.Library()
def sizify(value):
"""
Simple kb/mb/gb size snippet for templates:
{{ product.file.size|sizify }}
"""
#value = ing(value)
if value < 512000:
value = value / 1024.0
ext = 'kb'
elif value < 4194304000:
value = value / 1048576.0
ext = 'mb'
else:
value = value / 1073741824.0
ext = 'gb'
return '%s %s' % (str(round(value, 2)), ext)
register.filter('sizify', sizify)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Ah, well, I had no clue there already existed a tag for this. And so many more! Well, that settles that. Thanks btubbs.
#
Please login first before commenting.