def cache_if_anon(timeout):
"""Cache the view if the user is not authenticated and there are no messages to display."""
# https://stackoverflow.com/questions/11661503/django-caching-for-authenticated-users-only
def _decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated() or messages.get_messages(request):
return view_func(request, *args, **kwargs)
else:
return cache_page(timeout)(view_func)(request, *args, **kwargs)
return _wrapped_view
return _decorator
评论列表
文章目录