I have a website with a menu. To generate the menu, I need some queries in the database. So I created a context processor to do it in all my views.
Some of the views I have are actually forms. I get them using ajax and display them with jquery ui dialog, when my users click on some buttons.
I can't remove all context processors for those pretty complex forms, I need the auth, static and il8n context processors in particular.
But I don't want to make the menu based queries in the database to display those forms.
Is there a way to exclude a context processor in a view ?
I tried to put a variable in "request.session" in the view, then remove it and return an empty dictionary in my context processor. But it's pretty bad and there is possible concurrency issues.
I can also parse the url in "request" in my context processor and return an empty dictionary, but it sounds like an hack again.
Any idea or advice ?
This is what Django's lazy objects are for. Rather than calculate the actual contents in the context processor, you provide a lazy object with an associated function; when something actually tries to use the object, e.g. in a template, then it calls the function. This answer gives an example for the same problem.
Be careful about memoization if you use the variable more than once; some of the options will re-call the function, while some will save the result. You can look at the source to be sure. I think
SimpleLazyObject (as in the answer above) does what you want, but I haven't used this recently enough to be sure.
(An answer by request....)