diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index abb72fa13c9..6e522495843 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -118,15 +118,16 @@ class FirstOfNode(Node): self.asvar = asvar def render(self, context): + first = '' for var in self.vars: value = var.resolve(context, ignore_failures=True) if value: first = render_value_in_context(value, context) - if self.asvar: - context[self.asvar] = first - return '' - return first - return '' + break + if self.asvar: + context[self.asvar] = first + return '' + return first class ForNode(Node): diff --git a/tests/template_tests/syntax_tests/test_firstof.py b/tests/template_tests/syntax_tests/test_firstof.py index 1f382a6db31..cc8df211eba 100644 --- a/tests/template_tests/syntax_tests/test_firstof.py +++ b/tests/template_tests/syntax_tests/test_firstof.py @@ -82,3 +82,10 @@ class FirstOfTagTests(SimpleTestCase): output = self.engine.render_to_string('firstof15', ctx) self.assertEqual(ctx['myvar'], '2') self.assertEqual(output, '') + + @setup({'firstof16': '{% firstof a b c as myvar %}'}) + def test_all_false_arguments_asvar(self): + ctx = {'a': 0, 'b': 0, 'c': 0} + output = self.engine.render_to_string('firstof16', ctx) + self.assertEqual(ctx['myvar'], '') + self.assertEqual(output, '')