views:

375

answers:

3

I created a simple custom context processor that needs to only be run once per request. After putting in some logging hooks I found that it is being called twice per request.

Is this a known "feature" that a missed in the docs? Is it related to the number of templates in the inheritance tree? Is it a bug in 1.03?

Thanks.

+1  A: 

This is not expected behavior. Context processor are executed once each time a RequestContext is instantiated). In the case of template inheritance, the same context instance is passed up to the parent template, so that shouldn't cause another execution of the context processors. Either your logging is misleading (see @Vinay Sajip's comment), or you need to figure out where in your code an extra RequestContext might be executed on each request (are you using an inclusion tag or some other custom template tag that renders a template and instantiates RequestContext?)

EDIT Sorry, by "inclusion tag" I meant (in the generic sense) some tag that renders another template, not any tag that uses the inclusion_tag decorator. A regular inclusion_tag that takes context should simply pass along the existing context object, not instantiate a new RequestContext.

One thing you could try is to place an "import pdb; pdb.set_trace()" in your context processor, run the code in the Django dev server, and in the console examine the stack trace with pdb each time your context processor gets hit, to see where it's being called from.

Carl Meyer
I am using a custom inclusion tag that takes 'context' as an argument. Given that is this expected behavior? I hadn't realized that inclusion tags caused the context processor to be run again.
meppum
A: 

Is this happening on a production webserver, Apache etc, or just in the built in development server? I've noticed similar behaviour locally on occassion, but I'm pretty sure it's just a quirk in the runserver.

Greg
A: 

I figured out the issue. If a dictionary other than the original context is returned then the context processor seems to be executed again. Not sure why, and I can't be sure because I didn't look at the underlying code, but the after I updated the original context and returned that the issue went away. Thanks.

meppum