Question

I want to use TinyMCE in my Django project, after doing all the necessary settings, when I visit the page, the form only render normal django form but not TinyMCE rich text form. Below are my settings.

I copied tinymce folder to media folder:

   C:/Python27/Scripts/nate/media/js/tiny_mce

Settings.py

  TINYMCE_JS_URL = '/media/js/tiny_mce/tiny_mce.js/'

  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

urls.py

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

Models

    class Finmall(models.Model):
       user=models.ForeignKey(User)
       name=models.CharField(max_length=250, unique=True)
       content1 = models.TextField()
       content2 = models.TextField()

       def __unicode__(self):
           return self.user

Template

    <head>
     <script type="text/javascript" src="{{ MEDIA_URL }}/js/tiny_mce/tiny_mce.js"></script>
    </script>
    </head>

   {% block content %}

    <div id="postcribbody" class="wrapper">     

     <p class="list">You can make it</p>
      <form enctype="multipart/form-data"  form action="." method="POST">
          {% csrf_token %}
             {{ FinmallForm.as_p }}
  <input type="submit"  class="cribput" value="Submit"/>
      </form>
      </div>
   {% endblock %}

How can I make the content1 and content2 display TinyMCE rich text form?

Was it helpful?

Solution

An Example from the Documentation:

from django import forms
from tinymce.widgets import TinyMCE

class FinmallForm(forms.ModelForm):
    ...
    content = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
    ...

    class Meta:
        model = Finmall

OTHER TIPS

The problem is that you are displaying the form as a Django form.

The rich edit text field will be a JS thing which Django will not create. The django.form feature will be create a HTML element.

I suggest manually putting the rich edit in to the template. As long as you give the rich edit the same "name" as the Django form element you can use it in the POST which is returned.

EDIT: You are publishing a models.TextField() when the form is rendered. This will generate a element in the form. This form will have only default properties.

I am not 100% sure how TinyMCE works but you would need to bind it to that element though a "id" or "name" value or just put the forms code in yourself into the template.

I myself do not use Django to render the forms I create forms myself in the template and give them the same names what Django would give them.

Look at the source code for the rendered page and see what the form looks like. Then set up a test page with TinyMCE and get it working and look at the difference between the element.

:)

Try to put above form {{ form.media }} i.e. {{ FinmallForm.media }} in your template

    <head>
     <script type="text/javascript" src="{{ MEDIA_URL }}/js/tiny_mce/tiny_mce.js"></script>
    </script>
    </head>

   {% block content %}

    <div id="postcribbody" class="wrapper">     

     <p class="list">You can make it</p>
      {{ FinmallForm.media }}
      <form enctype="multipart/form-data"  form action="." method="POST">
          {% csrf_token %}
             {{ FinmallForm.as_p }}
  <input type="submit"  class="cribput" value="Submit"/>
      </form>
      </div>
   {% endblock %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top