Sunday, December 14, 2008

Django 1.x and tinyMCE

There is a lot of miss-documentation out on the web right now about how to setup wysiwyg editors to work with the django Admin scaffolding.

The current production release of django (1.0.2) at the time of this article is using the django newforms library for the admin scaffolding. I think at somepoint leading right up to the 1.0 major release there was two ways of customizing this newforms admin scaffolding. One involved including a special Admin class inside of all of your model classes
ex:
(from models.py)
class StaffBio(models.Model):
name = models.CharField("Name", maxlength=128,db_index=True)
position = models.CharField("Position", maxlength=128,db_index=True)
bio = models.TextField("Biography")
fote = ImageField("Photo", null=True,
blank=True, upload_to="E:/home/dev/storage/StaffBio/fote")
pageorder = models.IntegerField("Appearance on bio page",
choices=SITEORDER_CHOICES, db_index=True)
jobcat = models.CharField(maxlength=1,
choices=JOBCAT_CHOICES, db_index=True, default='P')

def __repr__(self):
return self.name

class Meta:
ordering = ['name', 'jobcat', 'status']
verbose_name = "Staff Bio"

class Admin:
fields = (
('Content', {'fields': ('name','position','bio','fote', 'jobcat', 'pageorder')}),
)
list_display = ( 'name', 'jobcat' )
list_filter = ['status', 'jobcat']
search_fields = ['name']
js = ['tiny_mce/tiny_mce.js', 'js/textareas.js']


and the other (currently best practice way) of doing it involved creating a new special configuration file called admin.py and putting it in the same directory as your models.py
ex:
(from admin.py)
from mew.cms.models import ArtistsInResidencePage

from django.contrib import admin

class ArtistsInResidencePageAdmin(admin.ModelAdmin):
class Media:
js = ('tiny_mce/tiny_mce.js', 'textarea.js')

admin.site.register(ArtistsInResidencePage, ArtistsInResidencePageAdmin)

so to get tiny mce to work, upload it to your media folder and then create new classes inside your admin.py file called ModelNameAdmin then register both the original model and it's Admin counterpart witha call do admin.site.register(ModelName, ModelNameAdmin)

remember that the location of the .js files is relative to the /media/ folder meaning that whatever you put in for the two values here:
js = ('tiny_mce/tiny_mce.js', 'textarea.js') will have the word /media/ prepended to it.

1 comment:

Danilo Cabello said...

Thanks, that was exactly what I was looking for.