Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.x] Fixed #17778 -- Prevented class attributes on context from re…

…solving as template variables.

Thanks KyleMac for the report, regebro for the patch, and Aymeric for the test.

Backport of 71b5617 from master.
  • Loading branch information...
commit ccff25b1431bd1bb9d633b1ca1d3aff79acc33d9 1 parent d2419bb
Tim Graham authored August 12, 2013
5  django/template/base.py
@@ -5,7 +5,7 @@
5 5
 from inspect import getargspec
6 6
 
7 7
 from django.conf import settings
8  
-from django.template.context import (Context, RequestContext,
  8
+from django.template.context import (BaseContext, Context, RequestContext,
9 9
     ContextPopException)
10 10
 from django.utils.importlib import import_module
11 11
 from django.utils.itercompat import is_iterable
@@ -765,6 +765,9 @@ def _resolve_lookup(self, context):
765 765
                     current = current[bit]
766 766
                 except (TypeError, AttributeError, KeyError, ValueError):
767 767
                     try:  # attribute lookup
  768
+                        # Don't return class attributes if the class is the context:
  769
+                        if isinstance(current, BaseContext) and getattr(type(current), bit):
  770
+                            raise AttributeError
768 771
                         current = getattr(current, bit)
769 772
                     except (TypeError, AttributeError):
770 773
                         try:  # list-index lookup
11  tests/template_tests/test_context.py
... ...
@@ -1,5 +1,5 @@
1 1
 # coding: utf-8
2  
-from django.template import Context
  2
+from django.template import Context, Variable, VariableDoesNotExist
3 3
 from django.utils.unittest import TestCase
4 4
 
5 5
 
@@ -14,3 +14,12 @@ def test_context(self):
14 14
         self.assertEqual(c.pop(), {"a": 2})
15 15
         self.assertEqual(c["a"], 1)
16 16
         self.assertEqual(c.get("foo", 42), 42)
  17
+
  18
+    def test_resolve_on_context_method(self):
  19
+        # Regression test for #17778
  20
+        empty_context = Context()
  21
+        self.assertRaises(VariableDoesNotExist,
  22
+                Variable('no_such_variable').resolve, empty_context)
  23
+        self.assertRaises(VariableDoesNotExist,
  24
+                Variable('new').resolve, empty_context)
  25
+        self.assertEqual(Variable('new').resolve(Context({'new': 'foo'})), 'foo')

0 notes on commit ccff25b

Please sign in to comment.
Something went wrong with that request. Please try again.