Django: use TinyMCE 4 in admin interface

After several ways to use TinyMCE as an editor for my HTML content in Django administration, I finally get it to work with this tutorial - https://github.com/ITCase-django/django-tinymce-4 .

However, using this method, I must have the Django 1.9 and Grappelli admin panel, which I would prefer to avoid. I tried to remove it, but it also uninstalled TinyMCE.

I also tried using the package HTMLField()from django-tinymce, but this method turned out to be a very rude editor with only a few options (bold, italics, lists, and that was).

Is there a “best practice” for administering Django (latest version) with full-featured TinyMCE 4?

EDIT: after you tried to use various parameters (for example, an extended theme with HTMLField ()), I returned to where I started the Grappelli theme. I think I can come to terms with this topic for some time.

+4
source share
3 answers

A third-party library is a quick fix, but if you only need a JS solution and you don't need to install any other library. You can do this in your django admin using your custom JS file.

class FooForm(forms.ModelForm):

   def __init__(self,*args,**kwargs):
      super(FooForm, self).__init__(*args, **kwargs)
      self.fields['yourtinymcefield'].widget.attrs['class'] = 'tiny-class'
   class Meta:
      model = FooModel
      fields = ('fields') # your fields here

Then in your admin.py

class FooAdmin(admin.ModelAdmin):
    form = FooForm
    # regular stuff
    class Media:
        js = (
            'https://cloud.tinymce.com/stable/tinymce.min.js' # tinymce js file
            'js/myscript.js',       # project static folder
        )

admin.site.register(Foo, FooAdmin)

Then initialize it in myscript.js

<script>

tinyMCE.init({
        //mode : "textareas",
        mode : "specific_textareas",
        editor_selector : "tiny-class",
        theme : "simple"
    });
</script>
+1
source
+1
source

Source: https://habr.com/ru/post/1676903/


All Articles