Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding tinyMCE in django flatpage

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')),
like image 785
creative creative Avatar asked Jul 23 '26 01:07

creative creative


1 Answers

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)
like image 95
Francis Yaconiello Avatar answered Jul 25 '26 14:07

Francis Yaconiello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!