Login

TinyMCE Widget

Author:
semente
Posted:
August 28, 2009
Language:
Python
Version:
1.1
Score:
1 (after 1 ratings)

Widget for TinyMCE 3.2.6, a WYSIWYG HTML editor for textarea.

Note:

This snippet uses the TinyMCE package thats contains special jQuery build of TinyMCE and a jQuery integration plugin. Anyway, is easily to adapt to standard package.

Usage example:

from django.contrib.flatpages.admin import FlatpageForm
class MyFlatPageForm(FlatpageForm):
    content = forms.CharField(widget=TinyMCEEditor())

TinyMCE download page

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from django import forms
from django.conf import settings
from django.utils.safestring import mark_safe

class TinyMCEEditor(forms.Textarea):

    class Media:
        js = (
            'js/jquery.js',
            'js/tiny_mce/jquery.tinymce.js',
        )

    def render(self, name, value, attrs=None):
        rendered = super(TinyMCEEditor, self).render(name, value, attrs)
        return rendered + mark_safe(u'''<script type="text/javascript">
    jQuery('#id_%s').tinymce({
        script_url : '%sjs/tiny_mce/tiny_mce.js',
        mode : "textareas",
        convert_urls : false,
        width:  585,
        height: 380,
        theme : "advanced",
        plugins : "table,searchreplace",
        theme_advanced_buttons1 : "bold,italic,underline,separator,link,unlink,image,strikethrough,separator,bullist,numlist,separator,indent,outdent,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,undo,redo,separator,formatselect,separator,search,replace,separator,code",
        theme_advanced_buttons2 : "tablecontrols",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_path_location : "bottom",
        extended_valid_elements : "a[name|href|target|title|onclick]"
    });
</script>''' % (name, settings.MEDIA_URL))

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

valerio.maggio (on February 27, 2012):

Great Snippet! Simple and Clean!

However, I would like to point out that, if used in Django 1.3x+, it requires to substitute settings.MEDIA_URL with settings.STATIC_URL.

Cheers

#

Please login first before commenting.