Django: How to ensure the function name and context name are the same for context processing

Sep 17, 2009 10:09


We frequently write little functions that populate the Django context, and sometimes we want that context to be site-wide, and we want every page and every Ajax handler, basically everything that takes a request and spews a response, in our application to have access to that information.  It might the user’s authentication, or his authorization, or ( Read more... )

django, python

Leave a comment

Comments 2

tamino September 17 2009, 19:25:52 UTC
I wonder what the performance characteristics of that are, versus something like:

def context_processor(f):
name = f.__name__
return lambda request: {name: f(request)}

@context_processor
def need_browser_warning(request):
return not adequate_browser(request.META.get('HTTP_USER_AGENT'))

Reply

elfs September 17 2009, 20:06:44 UTC
The inspect method is significantly faster. Invoking the two with the profile active shows that the indirection of the decorator doubles run-time commitment.

Reply


Leave a comment

Up