__getitem__ and lookups in templates

2009-08-31 Thread efege

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@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
-~--~~~~--~~--~--~---



Context problem - From list to details and back

2008-03-07 Thread efege

Hi, I'm new to Django and Python too, and just started planning the
migration of an existing app to Django, after reading (though not very
carefully) The Definitive Guide to Django. Excellent documentation,
BTW!

I'd like to know which is the recommended approach to handle this
situation in Django -- though I'm not sure how Django-specific my
question is.

A user does a search, the result is a paginated list of records, and
each record on the list has a link to a details page for that record.
Once in the details page, the user has these 3 navigation links:

  A. Go back to the list (i.e., to the same results page where the
current record appears, preserving list sort order)
  B. Go to the details page for the previous record in the list
  C. Go to the details page for the next record in the list

Also, the user can stay on the current record, but changing its
display style among several available styles. Whatever the chosen
style, the above 3 links must be present.

So the problem is: how do you keep the context (search query, and
record's position in results list), so that the above 3 links can be
generated from the details view/template?

FYI, my app originally saved that context in the URL used to request a
details page, thus creating ugliness such as

   ...&task=display_record¤t=2&total=13&queryID=/tmp/filesbdfi4

to display the 2nd record in a list of 13 results, where 'current' is
the index of the current record in the list, 'total' is the length of
the list, and 'queryID' points to a temp file where the query results
are cached.

Is the scenario I described common enough to have some known name? Can
the Django's session framework help me in this situation to keep the
URLs short and clean? What approach do you suggest?

Thank you in advance for sending some light in this direction :-)

--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@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---