在管理员中嵌入WYMEditor

时间:2013-02-11 03:33:23

标签: django django-admin wymeditor

如何将WYMEditor添加到管理面板?我正在将它用于我的用户到用户消息传递应用程序,我想将我的textarea替换为文本编辑器。

2 个答案:

答案 0 :(得分:5)

首先,在这里下载WYMEditor包http://www.wymeditor.org/download/

创建 widgets.py

from django import forms
from django.conf import settings
from django.utils.safestring import mark_safe

class WYMEditor(forms.Textarea):
    #change this for the exact location of your package in your staticfiles
    class Media:
        js = (
            'js/jquery.js', 
            'wymeditor/jquery.wymeditor.pack.js',
        )

    def __init__(self, language=None, attrs=None):
        self.language = language or settings.LANGUAGE_CODE[:2]
        self.attrs = {'class': 'wymeditor'}
        if attrs:
            self.attrs.update(attrs)
        super(WYMEditor, self).__init__(attrs)

    def render(self, name, value, attrs=None):
        rendered = super(WYMEditor, self).render(name, value, attrs)
        return rendered + mark_safe(u'''<script type="text/javascript">
            jQuery('#id_%s').wymeditor({
                updateSelector: '.submit-row input[type=submit]',
                updateEvent: 'click',
                lang: '%s',
            });
            </script>''' % (name, self.language))

更新您的forms.py以获取邮件表单,如下所示:

from app_name.widgets import WYMEditor

class MessageAdminForm(forms.ModelForm):
    .....
    body = forms.CharField(widget=WYMEditor())
    .....

就是这样,记得把你的包的确切位置

答案 1 :(得分:1)

我建议使用像django-wymeditor这样的东西,而不是重新发明这个轮子。

要使用此功能,请执行included example,只需添加admin.py

from django.contrib import admin
from wymeditor.admin import RichTextAdmin
from example.models import TestModel

class TestModelAdmin(RichTextAdmin):
    pass

admin.site.register(TestModel, TestModelAdmin)