Preventing django-cms’s toolbar from showing on certain urls (updated 2011-05-23)

Tags: django

Django-cms has a toolbar at the top of the page when you’re editing. Handy for adding/editing pages or for going to the admin interface. It does that by means of some django middleware that injects (mostly) javascript code at the top of your page. Fun.

Only, you don’t want it everywhere if you’ve got non-django-cms parts of your site. See for instance a recent mailinglist discussion.

There’s currently no build-in way to disable it, but doing that yourself turned out to be pretty simple. Updated: this didn’t work.

In my site, I originally added a mysite.hacked_toolbar.py:

from cms.middleware.toolbar import ToolbarMiddleware

START_OF_EXCLUDED_URLS = ['/map/', '/share/']
# ^^^ Modify to your needs.


class HackedToolbarMiddleware(ToolbarMiddleware):
    """Hacked version of django-cms's toolbar middleware.

    This modification prevents /map and /share urls from
    getting a toolbar as django-cms's toolbar contains
    conflicting jquery stuff, leading to javascript errors
    in our own part of the application.

    To use it, instead of
    'cms.middleware.toolbar.ToolbarMiddleware', use
    'mysite.hacked_toolbar.HackedToolbarMiddleware' in
    your ``MIDDLEWARE_CLASSES``.

    """

    def show_toolbar(self, request, response):
        """Return False on excluded urls, use original method otherwise.
        """
        for url in START_OF_EXCLUDED_URLS:
            if request.path.startswith(url):
                return False
        return ToolbarMiddleware.show_toolbar(self, request, response)

Afterwards, in your settings.py I told you to go to the MIDDLEWARE_CLASSES. And switch 'cms.middleware.toolbar.ToolbarMiddleware' for 'mysite.hacked_toolbar.HackedToolbarMiddleware'. But don’t do that.

Update 2011-05-23. What you really have to do is some dirty monkeypatching as there’s apparently a hard-coded check for the right middleware name in django-cms, sigh. Discovered by two of my colleagues that rightly suspected my nice and clean solution as the culprit :-) They saw a comment on the mailinglist from after I posted my solution there.

So the hacked_toolbar.py becomes:

# -*- coding: utf-8 -*-
"""Hacked version of django-cms's toolbar middleware.

This modification prevents /map and /share urls from getting
a toolbar as django-cms's toolbar contains conflicting
jquery stuff, leading to javascript errors in our own part
of the application.

To use it, assign hacked_show_toolbar() function to
toolbar.ToolbarMiddleware.show_toolbar, do it in
deltaportaal/urls.py.

"""

from cms.middleware.toolbar import ToolbarMiddleware

START_OF_EXCLUDED_URLS = ['/map/', '/opendap/', '/shape/']

orig_show_toolbar = ToolbarMiddleware.show_toolbar


def hacked_show_toolbar(self, request, response):
    """Return False on excluded urls, use original method otherwise."""
    for url in START_OF_EXCLUDED_URLS:
        if request.path.startswith(url):
            return False
    return orig_show_toolbar(self, request, response)

And in the urls.py (that’s the handiest place to initialize such a thing):

...
from cms.middleware import toolbar
from your_project.hacked_toolbar import hacked_show_toolbar
toolbar.ToolbarMiddleware.show_toolbar = hacked_show_toolbar
...

And in your settings.py, you can just keep the regular django-cms toolbar middleware line. 2

 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):