Django app that provides a pygments plugin for tinymce and backend functionality. Particularly useful for Mezzanine

django, tinymce, pygments, mezzanine
pip install django_mce_pygments==0.1.3




Django app and tinymce extension that allows you to insert syntax highlighted sourcecode. It is configured to work by default with a Mezzanine installation. Syntax highlighting is provided by pygments. You can adjust line numbering, highlighted ranges, styles and every language your version of pygments supports is selectable. You can optionally select to have a link to download the unformatted source code inserted, in which case a text file containing the source is saved to your server. The default paths for tinymce and jquery are designed for a mezzanine installation, but can be customized in

This project is based on


The pygments icon in tinymce:

The pygments icon

The dialog:

The Dialog

The preview tab:

The Preview Tab

The inserted source with download link:

The inserted source with download link



it is assumed you are using django.contrib.staticfiles to manage static media

File path defaults assume a Mezzanine installation but this can be easily overridden in your


Place the app on your python path

Add django_mce_pygments to your INSTALLED APPS

Check that the paths below for jquery and tinymce are correct, and override

in your settings file if neccessary

Make sure the SOURCE_CODE_FILE_DIR exists and is writable

Find your tinymce_setup.js file and add pygments to the plugins line, here is an example:

plugins: "inlinepopups,contextmenu,tabfocus,searchreplace,fullscreen,advimage,advlink,paste,media,table,pygments,spellchecker"

Also add it to your theme, here is an example:

theme_advanced_buttons1: "bold,italic,|,link,unlink,|,image,|,media,charmap,|,code,|,table,|,bullist,numlist,blockquote,|,undo,redo,|,formatselect,|,search,replace,|,pygments,|,fullscreen,",

If you are using mezzanine and static files, the plugin should be available by default. If it does not load, copy the django_mce_pygments/static/grappelli/tinymce/jscripts/tiny_mce/plugins/pygments directory into your tinymce plugins directory.

Finally add the pygments urls to your In a mezzanine installation, make sure this is before the mezzanine.urls include, as this includes a catchall. Example:

("^pygments/", include("django_mce_pygments.urls")),
("^", include("mezzanine.urls")),

If you are running Mezzanine, it automatically strips out inline styles which breaks pygments source highlighting, you can turn this off in your file as follows:



The following can be set in your All are optional, but the first two probably need adjustment if you are not using Mezzanine.

MCE_POPUP_LOCATION - The location of your tiny_mce_popup.js file, defaults to 'grappelli/tinymce/jscripts/tiny_mce/tiny_mce_popup.js'. This assumes you are using static files, so this url will be passed through the static template tag, and probably resolve to /staticgrappelli/tinymce/jscripts/tiny_mce/tiny_mce_popup.js.

JQUERY_LOCATION - The location of your jquery library. Defaults to 'mezzanine/js/jquery-1.7.1.min.js'. This assumes you are using static files, so this url will be passed through the static template tag, and probably resolve to /static/mezzanine/js/jquery-1.7.1.min.js

MCE_DEFAULT_LEXERS - Languages in this list will be the first in the dropdown in the pygments dialog to save you time hunting. Defaults to an empty list, an example would be:

MCE_DEFAULT_LEXERS = ['Python', 'Javascript']

MCE_DEFAULT_STYLE - The default style to have selected in the pygments dialog. This defaults to None. An example of its use:


SOURCE_CODE_FILE_DIR - The location to store raw source code files used for lines. The default is static/source, make sure it exists and is writable.

The template pygments/source_link.html can be overridden or customized to change how the download link for source code is presented.