On Mon, Aug 31, 2009 at 3:48 PM, efege <fjgo...@gmail.com> wrote: > > Hi, > > According to the docs, > > "... when the template system encounters a dot in a variable name, it > tries the following lookups, in this order: > > * Dictionary lookup. Example: foo["bar"] > * Attribute lookup. Example: foo.bar > * Method call. Example: foo.bar() > * List-index lookup. Example: foo[bar] > > The template system uses the first lookup type that works. It's short- > circuit logic." > > I wonder what's the exact meaning of "works" in the last paragraph... > > This is my problem: my template's variables are instances of a class > which has a special method __getitem__ defined. It also has a method > foo. So, when I want my template to invoke the method foo of that same > class, I wrote something like > > someobject.foo > > And guess what... dictionary lookup, i.e. __getitem__('foo'), takes > precedence, and so the method foo is not invoked! > > In my case, __getitem__ only accepts as valid certain specific > arguments, and 'foo' is not valid, so that __getitem__('foo') might > raise an exception, or return None. But in any case, the fact is that > Django *stops* trying the other possible lookups. >
Your __getitem__ might raise an exception, or return None? Which does it do? Returning None will be seen by the template system as "working", as it is perfectly valid for None to be a value stored in a dictionary. You will need to make this method raise an appropriate exception to make the template system consider the dictionary lookup to have failed. Here's where the template code tries these different lookups in order: http://code.djangoproject.com/browser/django/tags/releases/1.1/django/template/__init__.py#L712 Based on that code, a dictionary lookup that raises TypeError, AttributeError, or KeyError will cause the template code to continue with trying an attribute lookup, so it is one of those three specifically that you will need to raise in the case where you want the dictionary lookup to be seen as failing. Karen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---