extending admin for wmd editor (markdown)

57 views
Skip to first unread message

mattias

unread,
Jul 31, 2008, 5:25:26 AM7/31/08
to Django users
I've got the actual wmd (http://wmd-editor.com/) editor working by
adding

class Media:
js = ('javascript/wmd-1.0.1/wmd/wmd.js',)

But what I cannot seem to workout is how to extend the admin templates
in a way so I can get the little showdown preview next to my textarea.

I'm trying to get this kind of behaviour next to my input.
http://wmd-editor.com/examples/splitscreen

Can this be done using django newforms admin?

Russell Keith-Magee

unread,
Jul 31, 2008, 5:43:33 AM7/31/08
to django...@googlegroups.com

Sure. I'm not particularly familiar with wmd, but by the looks of it,
when you include the wmd script, it replaces any text area on the page
with the wmd widget. A preview area is created when a <div
class="wmd-preview"></div> is found.

What you need to do is write a custom Textarea widget that overrides
the base Textarea widget, modifying the render() method to include
both the <textarea> and the <div>.

Then, whenever you have a form with a text input, specify your custom widget.

If you associate the Media with the widget, any time you use the
widget, the javascript will automatically get included on the admin
page.

Yours,
Russ Magee %-)

mattias

unread,
Jul 31, 2008, 7:32:06 AM7/31/08
to Django users
Thanks Russ got it working!
This is how i did it:

-- custom_widgets.py --

import django.newforms as forms
from django.utils.safestring import mark_safe

class RichTextEditorWidget(forms.Textarea):

class Media:
js = ('javascript/wmd-1.0.1/wmd/wmd.js',)

def render(self, name, value, attrs=None):
output = [super(RichTextEditorWidget, self).render(name, value,
attrs)]
output.append(u'<div id="previewPane" class="pane wmd-preview"></
div>')
return mark_safe(u''.join(output))


-- admin.py --

class PostAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'shortdesc':
kwargs['widget'] = RichTextEditorWidget
return
super(PostAdmin,self).formfield_for_dbfield(db_field,**kwargs)


I think the hook in PostAdmin is a bit clunky but it works.

Thanks again!


On Jul 31, 10:43 am, "Russell Keith-Magee" <freakboy3...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages