0

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

4

2 回答 2

5

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

创建小部件.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())
    .....

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

于 2013-02-11T03:39:03.063 回答
1

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

要使用它,以包含的示例为例,只需添加一个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)
于 2013-02-11T16:33:59.177 回答