veni vidi Scripsi

Posts Tagged ‘textfield’

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. 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 download from tinymce.moxiecode.com/) is also in you site media js folder (/[sitemedia]/js/tiny_mce/) or change the TINYMCE_JS_URL to the correct folder in your settings.py.
  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
  5. 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)

June 21, 2010

No Comments »

Tags: , , , , , ,

Category: General

Attached Images: