From 92151b2d283ee46dfaf35ec93c41ffabce58e850 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Tue, 7 Nov 2006 04:58:10 +0000 Subject: [PATCH] Fixed #2914: filesizeformat no longer dies on invalid values. Thanks, dev@simon.net.nz git-svn-id: http://code.djangoproject.com/svn/django/trunk@4044 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/defaultfilters.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index cf1d3d5f6d..969ef7b28b 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -421,7 +421,11 @@ def filesizeformat(bytes): Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102 bytes, etc). """ - bytes = float(bytes) + try: + bytes = float(bytes) + except TypeError: + return "0 bytes" + if bytes < 1024: return "%d byte%s" % (bytes, bytes != 1 and 's' or '') if bytes < 1024 * 1024: