Basically both approaches are quite similar. request.session
approach can just get eliminate an extra step with setting up a caching in middleware manually. The bad thing about session approach is that the data is not guaranteed to be available if you use cache storage out of box. For example, there can be 0.1% of users with disabled cookies, you are using memcache storage there can be a necessity to restart your memcache server on redeploy and your logged-in users will lost their data, etc. In summary, I would not use sessions with cache storage for crucial data. In my projects I prefer to use 1st option as it gives me a greater level of control.
You can set up have special middleware adding view_name
variable to the request and enabling to control where and what to show:
class NameMiddleware(object):
def process_view(self, request, view_func, view_args, view_kwargs):
# add current view
if isinstance(view_func, str):
request.view_name = view_func
elif hasattr(view_func, '__name__'):
request.view_name = view_func.__name__
Then, you will have a greater control on where to provide additional information to the request, for example in context_processors you can attach department inf only to the selected views and cache a result in an efficient way(proxing a request to the database if you want):
def department_context_processor(request):
if hasattr(request, 'view_name'):
if request.view_name == 'department_view1' or request.view_name == 'department_view2':
departments = cache.get('department_'+str(request.user), None)
if departments is None:
departments = Department.objects.filter(user=request.user)
cache.set('department_'+str(request.user), departments, 60*60)
if departments:
return {
'departments': departments
}
return {}