TinyMce for specific field / widget?

263 views
Skip to first unread message

step

unread,
May 27, 2010, 7:57:06 PM5/27/10
to Django Grappelli
Does anyone have have an stratetgies for apply tiny to specific text
fields?

IE I have two in my model, summary and body text, i just need tinymce
on bodytext...

If I use

class Media:
js = ['/media/admin/tinymce/jscripts/tiny_mce/tiny_mce.js', '/
path/to/your/tinymce_setup.js',]

in the model admin, both text fields are editable via tinymce...

I tried to get it work using the django-tinymce but there doesn't seem
to be a way to "easily" include the grappelli tinymce config file...


Maxime Haineault

unread,
May 27, 2010, 11:43:23 PM5/27/10
to django-g...@googlegroups.com
I noticed this issue too .. but never really had to deal with it.

There is a class you can add to prevent tinyMCE from initializing on a field, it's somthing like "noMceEditor" or "no-mceeditor"
I can't remember.. and of course it implies that you write your form manually..

I think the "right" way to do it would be to create a custom form field which inherits a text field..

something like

class MyModel(models.Model):
    body  = models.RichTextField(...)

anything else is somewhat a hack imo.

--

 Maxime Haineault
 Consultant Web / Associé

∞ Motion Média  
  http://motion-m.ca
  m...@motion-m.ca
  (450) 747-0862


2010/5/27 step <ste...@gmail.com>

Rob Yates

unread,
May 28, 2010, 8:49:02 AM5/28/10
to django-g...@googlegroups.com
On 5/27/10 7:57 PM, step wrote:
> Does anyone have have an stratetgies for apply tiny to specific text
> fields?
>
> IE I have two in my model, summary and body text, i just need tinymce
> on bodytext...
>
Assuming you are just referring to the standard admin interface and not
your own forms/views, you can exclude fields by overriding the
formfield_for_dbfield method on your ModelAdmin class. It's not pretty
or semantic, but it works.

So assuming a model called Story with a summary and a body, it would
look something like this in admin.py:

class StoryAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
field = super(StoryAdmin, self).formfield_for_dbfield(db_field,
**kwargs)
if db_field.name == 'summary':
field.widget.attrs['class'] = 'mceNoEditor ' +
field.widget.attrs.get('class', '')
return field

Any fields you don't explicitly exclude in that if statement will get
the TinyMCE treatment. Hope that helps.

-Rob

patrickk

unread,
May 30, 2010, 12:52:00 PM5/30/10
to Django Grappelli
set mode to "exact" and elements to the ID of your bodytext-field, see
http://wiki.moxiecode.com/index.php/TinyMCE:Configuration/mode

regards,
patrick

Julien Bidoret

unread,
Aug 31, 2011, 5:47:46 AM8/31/11
to django-g...@googlegroups.com
Patrick's answer seems to be the good way ;
Assuming your models define a summary and a text, and you want TinyMce to be applied to text only, you can set :

tinyMCE.init({
       
...
        mode
: "exact",
        elements
: "id_text"
});

In your '/path/to/your/tinymce_setup.js'. Ids for elements are defined from your fields names.

— julien
Reply all
Reply to author
Forward
0 new messages