summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/templatetags/i18n.py7
-rw-r--r--tests/template_tests/syntax_tests/i18n/test_blocktranslate.py9
2 files changed, 16 insertions, 0 deletions
diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py
index d0fda0797e..93023f3109 100644
--- a/django/templatetags/i18n.py
+++ b/django/templatetags/i18n.py
@@ -1,3 +1,5 @@
+from decimal import Decimal
+
from django.conf import settings
from django.template import Library, Node, TemplateSyntaxError, Variable
from django.template.base import TokenType, render_value_in_context
@@ -135,6 +137,11 @@ class BlockTranslateNode(Node):
singular, vars = self.render_token_list(self.singular)
if self.plural and self.countervar and self.counter:
count = self.counter.resolve(context)
+ if not isinstance(count, (Decimal, float, int)):
+ raise TemplateSyntaxError(
+ "%r argument to %r tag must be a number."
+ % (self.countervar, self.tag_name)
+ )
context[self.countervar] = count
plural, plural_vars = self.render_token_list(self.plural)
if message_context:
diff --git a/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py b/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py
index 8edacd87e2..5079824b30 100644
--- a/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py
+++ b/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py
@@ -299,6 +299,15 @@ class I18nBlockTransTagTests(SimpleTestCase):
self.engine.render_to_string('template', {'a': [1, 2, 3]})
@setup({'template': (
+ '{% load i18n %}{% blocktranslate count counter=num %}{{ counter }}'
+ '{% plural %}{{ counter }}{% endblocktranslate %}'
+ )})
+ def test_count_not_number(self, tag_name):
+ msg = "'counter' argument to '{}' tag must be a number.".format(tag_name)
+ with self.assertRaisesMessage(TemplateSyntaxError, msg):
+ self.engine.render_to_string('template', {'num': '1'})
+
+ @setup({'template': (
'{% load i18n %}{% blocktranslate count count=var|length %}'
'There is {{ count }} object. {% block a %} {% endblock %}'
'{% endblocktranslate %}'