Update July 27, 2010:
- 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:
- 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,”.
- 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:
- 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.
- Download the latest version of Django-TinyMCE from http://pypi.python.org/pypi/django-tinymce/ (I’m on 1.5 now).
- 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-).
- 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/.
- 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.
- 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/.
- Finally put the following line in your urls.py in your Django project: (r’^tinymce/’, include(’tinymce.urls’)),.
- Add ‘tiny_mce’, to your installed apps in settings.py of your Django project. Correction: this should be ‘tinymce’ without the underscore.
- 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!