Does anyone know a good way to solve this other problem I have. My site shows menus based on a users privs. I have a function that returns the privs as a dictionary as below:
return {"manage_entries":True, "manage_members":False,
"manage_something_else":True}
I passed in each priv to my base template that includes the navigation bar and use simple {% if priv %} to decide if I am going to show the menu item or not. It works fine except…
I need to pass the privs in the context of every view as they all include the base.html template and thus menu. There are lots of views so this is stupid. There must be a better way!
Cheers
Rich
One way to do this is to write a simple custom template context processor. This is as simple as a function that accepts an instance of
HttpRequestand returns a dictionary. In your case the dictionary can contain a list of the privileges for the current user.For e.g.
Where
get_privsis the method that will return the dictionary of privileges as you have indicated in your question.Now add this processor to the
TEMPLATE_CONTEXT_PROCESSORSsetting in yoursettings.py. Generally this variable is not present in settings. When you add it, make sure that you copy the existing default and then append to it. For e.g.Finally in your base template expect the
privilegesvariable.This will make sure that all your views will automatically return a context with a correct
privilegesvariable.