CKEditor Widget for Django form
This is an example snipplet on integrating CKEditor into Django
Integrating CKEditor into django is easy! Just copy the following code into your forms.py then you get a CKeditorWidget that you can easily use in forms.
class CKeditorWidget(forms.Textarea):
attrs={}
def __init__(self, toolbar,height=250):
self.toolbar=toolbar
self.height=height
class Media:
js=('/site_media/ckeditor/ckeditor.js',)
def render(self, name, value, attrs=None):
if value is None:
value = ''
value = force_unicode(value) #important when pre-fill content contains unicode
rendered = super(CKeditorWidget,self).render(name, value, attrs)
return mark_safe(self.media)+ rendered+mark_safe(
u"""<script type="text/javascript">
$(function(){
CKEDITOR.replace('%s', {toolbar:'%s', height:'%s',
on:{instanceReady:function(ev){ #execute when CKEditor initiate, useful for setup codes
this.dataProcessor.writer.indentationChars='';
}}
});
});
</script>
"""%(name, self.toolbar,self.height))
Following example show how to use the CKeditorWidget, the toolbar value is the toolbar setting you want to use.
ckeditorfield=forms.CharField(label='CKEditor', widget=CKeditorWidget(toolbar='Simple',height=300))
Please login to post comment.