Short version:

Callable context variables do not get called during resolution. If
invoked as a simple variable they return something like  <function foo
at 0x8c454fc>. If used in a for loop, they blow up the template. I'm
running trunk:7374

Longer version:

In order to delay a *very* expensive DB operation, I decided to put it
in a function and pass the function as a context variable. The
template uses fragment caching with vary_on, so only if the template
fragment is stale would the function be called and invoke the heavy
lifting.

Sounded great to me ... but it blows up with a 500 error.

Simple example:
=========================
def some_view(request):
   def foo():
      return [1, 2, 3]
   return render_to_response('some_template.html',  {'foo':   foo, })

some_template.html:
   {{foo}} ==>> yields <function foo at 0x8c454fc>
   {% for x in foo %}{{x}}{% endif %} ==>> 500 error

=========================

The problem seems to be in (or is related to)
template.__init__.Variable._resolve_lookup.

[...]
        current = context
        for bit in self.lookups:
            try: # dictionary lookup
                current = current[bit]
## the following fixed the problem (but needs error handling?)
                if callable(current):
                    current = current()
##
            except (TypeError, AttributeError, KeyError):
                try: # attribute lookup
                    current = getattr(current, bit)
                    if callable(current):
                        if getattr(current, 'alters_data', False):
                            current =
settings.TEMPLATE_STRING_IF_INVALID
                        else:
                            try: # method call (assuming no args
required)
                                current = current()
[...]


Note there is a check for callable, but only if it's a method. Is
there some reason why callability is OK in some context variables/
expressions and not others?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@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-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to