veni vidi Scripsi

from the WEBLOG

 

TinyMCE in Django

Update July 27, 2010:

  1. If you get the following error (probably in your shell):
    Exception Value: cannot import name smart_unicode
    check this post for the solution http://code.google.com/p/django-tinymce/issues/detail?id=63.
    In short, in your tinymce folder (that is your django-tinymce installation in the python site-packages) edit the file widgets.py and remove the line that says
    from django.forms.util import smart_unicode
    and after the line that says
    from django.utils import simplejson
    add the following line
    from django.utils.encoding import smart_unicode
    You can see it in the diference file.

Update June 13, 2010:

  1. In Django 1.2 (or the new Django-TinyMCE) some of the TinyMCE plugins don’t work. In order to get TinyMCE to work I highly suggest either using the TinyMCE simple theme or removing all the plugins from the advanced theme and adding the ones you need one by one. The following plugins were safe in the most recent Django 1.2 project I did: “safari, spellchecker, save, advlink, iespell, paste, xhtmlxtras,”.
  2. Last week when I was using my own guide I found out that I had made a mistake in the guide. The mistake is in step 8 and it has been corrected.

So, I’m working with Django again. This blog is still in Wordpress, but won’t be for long.

A while ago when I was working with Django I had a really hard time trying to figure out how to get TinyMCE to work in Django. Now it isn’t difficult, you just have to know how to do it. So I wrote a quick guide on how to make it all work. I’m sure other people will have done this as well, but it took me a while to figure it out, so I thought: the more guides the better :) . Anyway, here it is:

  1. Download the latest TinyMCE from http://tinymce.moxiecode.com (I’m using version 3.2.7 a.t.m.). I used the non-jquery version, haven’t looked at the pros and cons of it, so if you would like to just get it working I suggest you try the normal version first.
  2. Download the latest version of Django-TinyMCE from http://pypi.python.org/pypi/django-tinymce/ (I’m on 1.5 now).
  3. Unzip both packages (in a terminal that’s: tar xzvf django-tinymce-1.5.tar.gz for the django-tinymce tar.gz package -don’t forget to replace the version number-).
  4. Copy tinymce/jscripts/tiny_mce (the first tinymce is the folder you get after unzipping the TinyMCE package) to python/site-packages/django/contrib/admin/media/js/. On a Mac your Python installation is probably in /Library/Python/[version number]/. I’ve therefore put it in /Libary/Python/2.6/site-packages/django/contrib/admin/media/js/ and you will get /Library/Python/2.6/site-packages/django/contrib/admin/media/js/tiny_mce. Note that if you can’t find your python installation it could also be in /sw/lib/.
  5. Go back to the unzipped folder of django-tinymce. CD into the folder and do sudo python setup.py install to install django-tinymce in your python site-packages.
  6. Now, using the examples from http://tinymce.moxiecode.com/examples/full.php create a textareas.js which will define your TinyMCE configuration. When you look at the source of the examples, make sure you only copy the part from tinyMCE.init({ to }); (right before </script>). Because you’re saving it as a .js file you don’t need the rest. Place this textareas.js file in python/site-packages/django/contrib/admin/media/js/tiny_mce/.
  7. Finally put the following line in your urls.py in your Django project: (r’^tinymce/’, include(’tinymce.urls’)),.
  8. Add ‘tiny_mce’, to your installed apps in settings.py of your Django project. Correction: this should be ‘tinymce’ without the underscore.
  9. And for each of the models that has a textarea that you would like to use TinyMCE for, you should put the following class in the admin class of the model: (sorry, can’t do tabs in Wordpress)class Media:“‘”place tab here”‘” js = (’/media/js/tiny_mce/tiny_mce.js’, ‘/media/js/tiny_mce/textareas.js’,)

Note: It’s probably advisable to put your textareas.js in your website media folder rather than the admin media folder, because you’ll probably want to change it on a per site/project basis. That means you will have to change the second path in the Media class in your models’ admin to the correct path. So for example it could be: js = (’/media/js/tiny_mce/tiny_mce.js’, ‘/sitemedia/js/tiny_mce/textareas.js’,) if you specified MEDIA_URL to be ‘/sitemedia/’

Also: Don’t forget to use the ’safe’ filter in your templates, otherwise it won’t render correctly in the browser (see the Django docs for more info: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe). You can also use TinyMCE for user-end forms, but in this case be careful with the safe filter. See the Django documentation for more on this.

I hope this is all clear. If you have any questions or remarks, please leave a comment. Don’t forget to dig a bit deeper into the http://tinymce.moxiecode.com website as there is a lot more you can do with TinyMCE.

Good luck and have fun with your new django TinyMCE installation!


6 Responses to “TinyMCE in Django”

  1. Hi,I did as you said, but the tinyMCE still can’t work in Django admin page.BTW, my Django’s version is the latest one 1.2.1 . Thanks.

    • Hi Carlos, in the latest version of Django some things have changed which causes this method not to work. I have actually come across this problem this week myself and worked out how you can make it work in Django 1.2.

      I’m very busy at the moment, but I will have written an update to this post on Sunday with the new guide.

      In the mean time, try this: one of the problems was that some of the plugins that you define in ‘textareas.js’ don’t work with the new Django (or Django-tinymce). Try removing all the plugins (or use the ’simple’ theme instead of ‘advanced’, again without plugins) and see if that works. Then start adding pluggins one by one to see which ones work and which don’t.
      Also it seems that in settings.py instead of adding ‘tiny_mce’ you have to add ‘tinymce’ without the underscore to INSTALLED_APPS (this might have been a mistake in my guide actually).
      I also noticed that on my MacBook Pro at work my Python folder was in /Library/ instead of /sw/lib/.
      Hope this helps!

  2. Thanks for this. Can you give an example of how you used the safe filter in your template? I can’t figure out how to apply it to the admin textareas.

  3. Thanks so much Heleen, now that I know I had the syntax right, I figured out that I was putting it on the admin side, when I should have been modifying the display template. (doh) Anyway, it was a big help, and everything is behaving as expected now!

Leave a Reply

October 29, 2009

Comments

Tags: , , , , , ,

Category: General

Attached Images: