Embedding tinyMCE in django flatpage

2019-05-25 04:06发布

问题:

I'm using django-tinymce. I'd like to know how to embed it in a flatpage in admin panel.

From the project's readme:

Add tinymce to INSTALLED_APPS in settings.py for your project:

INSTALLED_APPS = (
    ...
    'tinymce',
)

Add tinymce.urls to urls.py for your project:

urlpatterns = patterns('',
    ...
    (r'^tinymce/', include('tinymce.urls')),
)

My flatpage url :

url(r'^pages/', include('django.contrib.flatpages.urls')),

回答1:

you need to override the widget for the content field. To do this:

  1. extend the FlatpageForm ModelForm as PageForm
  2. extend the FlatPageAdmin to use the new PageForm

code example:

from django.contrib.flatpages.admin import FlatpageForm, FlatPageAdmin
from django.contrib.flatpages.models import FlatPage
## OOPS this is a custom widget that works for initializing
## tinymce instances on stacked and tabular inlines
## for flatpages, just use the tinymce packaged one.
#from content.widgets import TinyMCE 
from tinymce.widgets import TinyMCE


class PageForm(FlatpageForm):

    class Meta:
        model = FlatPage
        widgets = {
            'content' : TinyMCE(attrs={'cols': 100, 'rows': 15}),
        }


class PageAdmin(FlatPageAdmin):
    """
    Page Admin
    """
    form = PageForm

then unregister the old flatpage admin and reregister the new one

admin.site.unregister(FlatPage)
admin.site.register(FlatPage, PageAdmin)