3

I am using Django 1.8 with Python2.7. I have Installed TinyMCE using command "pip install django-tinymce" and included 'tinymcy' app in INSTALLED_APPS.

INSTALLED_APPS = (
    ...
    'tinymce',
    ...
)

Included in Project URL: urls.py

url(r'^tinymce/', include('tinymce.urls')),

TinyMCE Configuration in Settings.py File

TINYMCE_JS_URL = os.path.join(STATIC_PATH,"django-tinymce-master/tinymce/media/tiny_mce/tiny_mce_src.js")


TINYMCE_JS_ROOT = os.path.join(STATIC_PATH, "django-tinymce-master/tinymce")

#D:\Dropbox\dp\p2d18\opinion\static\django-tinymce-master\tinymce

TINYMCE_DEFAULT_CONFIG = {
'plugins': "table,spellchecker,paste,searchreplace",
'theme': "advanced",
'cleanup_on_startup': True,
'custom_undo_redo_levels': 10,
}
TINYMCE_SPELLCHECKER = True
TINYMCE_COMPRESSOR = True

models.py

class Post(models.Model):
    ...
    body = models.TextField()
    ...

forms.py

from django import forms
from django.contrib.auth.models import User
from blog.models import Post, Comment, Tag
from tinymce.widgets import TinyMCE

class PostForm(forms.ModelForm):
    ---
    body = forms.CharField(widget=TinyMCE(attrs={'cols': 50, 'rows': 30}))
    ---

views.py

def add_post(request):
    if request.method == 'POST':
        form1Post = PostForm(request.POST)
        form2Tags = TagForm(request.POST)

        if form1Post.is_valid() and form2Tags.is_valid():
            post = form1Post.save()
            tag_title_from_form = form2Tags['tag_title'].value().strip().lower()
            tag_title_from_form = tag_title_from_form.rstrip(',')
            striped_tags = tag_title_from_form.split(',')
            for t in striped_tags:
                received_tag = t.strip()
                tag, created = Tag.objects.get_or_create(tag_title=received_tag)
                post.tag_set.add(tag)
            return index(request)
        else:
            print (form1Post.errors)
            print (form2Tags.errors)
    else:
        form1Post = PostForm()
        form2Tags = TagForm()

Added tinymce js link in my base.html

<script src="{% static 'django-tinymce-master/tinymce/media/tiny_mce/tiny_mce.js' %}"></script>

Added tinymce in my add_post.html

{{ form1.media }}

Generated HTML of Textarea

<textarea class="tinymce" cols="50" data-mce-conf="{&quot;cleanup_on_startup&quot;: true, &quot;spellchecker_languages&quot;: &quot;Afrikaans=af,Arabic=ar,Asturian=as,Azerbaijani=az,Bulgarian=bg,Belarusian=be,Bengali=bn,Breton=br,Bosnian=bs,Catalan=ca,Czech=cs,Welsh=cy,Danish=da,German=de,Greek=el,+English / Australian English / British,     English=en,Esperanto=eo,Spanish / Argentinian Spanish / Mexican Spanish / Nicaraguan Spanish / Venezuelan, Spanish=es,Estonian=et,Basque=eu,Persian=fa,Finnish=fi,French=fr,Frisian=fy,Irish=ga,Galician=gl,Hebrew=he,Hindi=hi,Croatian=hr,Hungarian=hu,Interlingua=ia,Indonesian=id,Ido=io,Icelandic=is,Italian=it,Japanese=ja,Georgian=ka,Kazakh=kk,Khmer=km,Kannada=kn,Korean=ko,Luxembourgish=lb,Lithuanian=lt,Latvian=lv,Macedonian=mk,Malayalam=ml,Mongolian=mn,Marathi=mr,Burmese=my,Norwegian Bokmal=nb,Nepali=ne,Dutch=nl,Norwegian, Nynorsk=nn,Ossetic=os,Punjabi=pa,Polish=pl,Portuguese / Brazilian Portuguese=pt,Romanian=ro,Russian=ru,Slovak=sk,Slovenian=sl,Albanian=sq,Serbian / Serbian Latin=sr,Swedish=sv,Swahili=sw,Tamil=ta,Telugu=te,Thai=th,Turkish=tr,Tatar=tt,Udmurt=ud,Ukrainian=uk,Urdu=ur,Vietn amese=vi,Simplified Chinese / Simplified Chinese / Traditional Chinese / Traditional Chinese=zh&quot;, &quot;elements&quot;: &quot;id_body&quot;, &quot;language&quot;: &quot;en&quot;, &quot;spellchecker_rpc_url&quot;: &quot;/tinymce/spellchecker/&quot;, &quot;directionality&quot;: &quot;ltr&quot;, &quot;theme&quot;: &quot;advanced&quot;, &quot;strict_loading_mode&quot;: 1, &quot;mode&quot;: &quot;exact&quot;, &quot;custom_undo_redo_levels&quot;: 10, &quot;plugins&quot;: &quot;table,spellchecker,paste,searchreplace&quot;}" data-mce-gz-conf="{&quot;themes&quot;: &quot;advanced&quot;, &quot;languages&quot;: &quot;en&quot;, &quot;debug&quot;: false, &quot;diskcache&quot;: true, &quot;plugins&quot;: &quot;table,spellchecker,paste,searchreplace&quot;}" id="id_body" name="body" rows="30"></textarea>

TinyMCE is not yet working! Where I am making Mistake

Muhammad Ahmed
  • 428
  • 2
  • 6
  • 17
  • `data-mce-conf="{"...` - the `"` should instead be a real quote `'`. – xyres Mar 12 '16 at 08:38
  • @xyres I have pasted it from the browser (Inspect Object). Does it mean that the problem lies in TinyMCE backend? I have also checked replacing """ with " ' " in html page but not responding in the desired format. – Muhammad Ahmed Mar 12 '16 at 09:25
  • Does your html load the TinyMCE JavaScript (is there a link to it in the head section of your HTML page)? – wobbily_col Mar 12 '16 at 22:06
  • Yes it does. I've added : – Muhammad Ahmed Mar 13 '16 at 07:27
  • try using `from tinymce.models import HTMLField` and body = `forms.HTMLField(widget=TinyMCE(attrs={'cols': 50, 'rows': 30}))` - that will handle Django admin widget as well – lukeaus Mar 14 '16 at 01:33

2 Answers2

2

try this to initialize the tinyMCE fields on your add_post.html page

<script>
tinyMCE.init({
  mode: "textareas",  // to do all text areas
  // or
  selector: "#id_myfield",  // change this value according to your HTML
});
</script>
lukeaus
  • 11,465
  • 7
  • 50
  • 60
-1

I know you solved your problem, but as I was looking to find the answer to solve my problem of not showing my desired stuff in the frontend, I reply to you:

you just need to filter in the frontend, so add like this in your template file:

{{ your_view_context.your_model_field | safe }}
Dharman
  • 30,962
  • 25
  • 85
  • 135
bghad1
  • 89
  • 8