veni vidi Scripsi

TinyMCE in one of multiple Textarea fields in Admin

When I was working with Django the other day I had a model which contained 3 textareas (models.TextField()). I only wanted 2 of the 3 to use TinyMCE for mark-up in the Admin. I worked out how to achieve this and here’s a quick guide.

  1. Install TinyMCE as explained in an earlier post.

  2. Make sure the tiny_mce folder, the one you downloaded from tinymce.moxiecode.com, is also in your site-media javascript folder (/[site­me­di­a]/js/tiny_mce/) or change TINYM­CE_JS_URL in your settings.py to the correct folder.

  3. In your settings.py define what you want the default tinymce to look like, for example:

    TINYMCE_DEFAULT_CONFIG = {
             'plugins': "safari, spellchecker, save, advlink, iespell, paste, xhtmlxtras,",
             'theme': 'advanced',
             'theme_advanced_buttons1': "bold, italic, underline, strikethrough,|, justifyleft, justifycenter, justifyright, justifyfull,|, bullist, numlist,|, outdent, indent, blockquote,",
             'theme_advanced_buttons2':"undo, redo, |, link, unlink, anchor, cleanup, help, code, spellchecker",
             'theme_advanced_buttons3':"", 'theme_advanced_buttons4':"",
             'theme_advanced_toolbar_location' : "top",
             'theme_advanced_toolbar_align' : "left",
             'theme_advanced_statusbar_location' : "bottom",
             'theme_advanced_resizing' : 'true',
    }
    
  4. In your model admin file add from tinymce.widgets import TinyMCE and to the model’s admin class add:

    def formfield_for_dbfield(self, db_field, **kwargs):
            if db_field.name == '[your_field_name]': # you can add more fields here by using 'or'
            kwargs['widget'] = TinyMCE(attrs={'cols':80, 'rows': 30})
            try:
                    del kwargs['request']
            except KeyError:
                    pass return db_field.formfield(**kwargs)
            return super([model_admin_class_name],self).formfield_for_dbfield(db_field, **kwargs)
    

Edit 20 Oct. 2011: It should be noted that in the new version of TinyMCE only the English language file is included with the 'simple' and 'standard' themes. When you use 'language':"nl" for example in your TINYM­CE_DE­FAULT­_­CON­FIG you should go to the TinyMCE website and get the required languages and put them in the correct folder (tiny_mce/themes/simple/lang for example).

TinyMCE in Django » « Quick Templating System for HTML Page Mock-ups (Mac OS X specific)