Skip to content

Instantly share code, notes, and snippets.

@davidmarquis
Created August 7, 2015 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidmarquis/3ecd34b062b1799512cf to your computer and use it in GitHub Desktop.
Save davidmarquis/3ecd34b062b1799512cf to your computer and use it in GitHub Desktop.
Django CMS disable toolbar for specific URLs
"""
Hi-jacked version of Django CMS's toolbar middleware that
excludes the toolbar from specific URLs (as defined in settings).
To use it, replace 'cms.middleware.ToolbarMiddleware' with a
reference to this class.
Then set `CMS_TOOLBAR_EXCLUDED_URLS` in your Django settings:
```
CMS_TOOLBAR_EXCLUDED_URLS = [
'/shop/',
]
```
"""
from cms.middleware.toolbar import ToolbarMiddleware
from django.conf import settings
class URLFilteringToolbarMiddleware(ToolbarMiddleware):
def process_request(self, request):
if self._is_matched(request):
return
super().process_request(request)
def process_view(self, request, view_func, view_args, view_kwarg):
if self._is_matched(request):
return
return super().process_view(request, view_func, view_args, view_kwarg)
def process_response(self, request, response):
if self._is_matched(request):
return response
return super().process_response(request, response)
def _is_matched(self, request):
path = request.path
for url in settings.CMS_TOOLBAR_EXCLUDED_URLS:
if path.startswith(url):
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment