osdir.com
mailing list archive

Subject: Re: __getitem__ and lookups in templates - msg#02159

List: DjangoUsers

Date: Prev Next Index Thread: Prev Next Index
On Mon, Aug 31, 2009 at 3:48 PM, efege <fjgomez@xxxxxxxxx> 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@xxxxxxxxxxxxxxxx
To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Was this page helpful?
Yes No
Thread at a glance:

Previous Message by Date: click to view message preview

Re: ImportError: No module named transmeta in Django 1.0.3 under Fedora 11

On Mon, Aug 31, 2009 at 2:40 PM, Zico <mailzico@xxxxxxxxx> wrote: Yes, I did it. Now my transmeta is in:/opt/fixmystreet/contrib/transmeta/__init__.pyAnd, my stdimage is in:/opt/fixmystreet/contrib/stdimage/__init__.py But, nothing changed yet!!! Same error is coming:No module named transmeta The traceback might help since it would show the import that is triggering this.  If it is "from contrib.transmeta import something" then I don't understand why you'd still be having problems with the setup you describe above. 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@xxxxxxxxxxxxxxxx To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

Next Message by Date: click to view message preview

Re: Django and ZSI

2009/8/31 Julián C. Pérez <jcpmmx@xxxxxxxxx>: > > Anyone has experience with using web services in Django?? > I mean, not necessarily with ZSI but others methods... > Yes, we have many of the working. But this is no a Django issue is more a Python programming one. You can access to Soap web services using ZSI, SUDS, Soappy or whatever you like to use. Isolate the web service client and use the parts you need. We have worked with ZSI mainly, but actually we're moving to Suds. To act a a server we prefer to simplify and we're using xml over http or REST services. Both live perfectly with Django. -- Antoni Aloy López Blog: http://trespams.com Site: http://apsl.net --~--~---------~--~----~------------~-------~--~----~ 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@xxxxxxxxxxxxxxxx To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

Previous Message by Thread: click to view message preview

__getitem__ and lookups in templates

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. BTW, the data my app uses is not stored in a Django model. Maybe this is part of the price I must pay for doing unusual things? :-) --~--~---------~--~----~------------~-------~--~----~ 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@xxxxxxxxxxxxxxxx To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

Next Message by Thread: click to view message preview

Re: __getitem__ and lookups in templates

On Aug 31, 5:16 pm, Karen Tracey <kmtra...@xxxxxxxxx> wrote: > 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. Thank you Karen for pointing me to the right part of the code, and for such a clear explanation. -- Fernando --~--~---------~--~----~------------~-------~--~----~ 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@xxxxxxxxxxxxxxxx To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---
Sign up for updates to this mailing list. email:
Loading Comments...
Home | News | Patents | Sitemap | FAQ | advertise

Advertising by