Can't get CKEditor plugins to work in django

前端 未结 5 1707
后悔当初
后悔当初 2021-01-07 02:37

I am trying to get the CKEditor plugin, codesnippet, to work in the django admin but am unable to. CKEditor works if I don\'t define any CKEDIT_CONFIGS in my settings.py. It

相关标签:
5条回答
  • 2021-01-07 03:15

    The Code Snippet plugin has various dependencies each of which has sub-dependencies, i.e.:

    • Widget (has Line Utilities as a sub-dependency amongst others)
    • Dialog (also has sub dependencies)

    I had to as a minimum add Code Snippet, Widget and Line Utils in the ckeditor/plugins path to get it to work, as well as use the following setting to get the button to show up in the toolbar.

    CKEDITOR_CONFIGS = {
       'default': {
            'toolbar':[ ['CodeSnippet', ], ],
            'height': 400,
            'width': 900,
            'removePlugins': 'stylesheetparser',
            'extraPlugins': 'codesnippet',
       },
    }
    

    So once all your plugin dependencies are all installed it should work.

    0 讨论(0)
  • 2021-01-07 03:16

    I've been battling the same problem for days and I guess I found a workaround for this problem.

    As you have noticed too, it tries to read this "static/ckeditor/ckeditor/plugins/codesnippet/plugin.js" javascript but it cannot locate it, even if you've put the plugin in the folder of "YOUR_PROJECT_DIR/static/ckeditor/ckeditor/plugins". The reason is, django-ckeditor is not searching the static directory in your project directory, it is searching it own static directory in its own path in site-packages. As a result, you may do the following as a workaround.

    1. Build CKEditor with your plugins (extra plugins like CodeSnippet) using its Builder, replace CodeSnippet plugin and its dependencies with the standalone versions downloaded seperately from the CKEditor website. (The plugins do not have plugin.js file in their folder)
    2. Download it and unzip it, you will have a folder named 'ckeditor' with 'lang', 'plugins' as subfolders
    3. Replace the entire 'ckeditor' directory in 'static/ckeditor/ckeditor' in the folder of 'ckeditor' in your python's site-package folder. For example, your django-ckeditor is installed in "C:\Python27\Lib\site-packages", you'll see 'ckedior', replace the 'static/ckeditor/ckeditor' folder with your built ckeditor folder. Or you will have virtualenv or whatever, you may do it in its own site-packages.
    4. Add 'extraPlugins' settings as you already did in the problem description, and run python manage.py runserver and you will see 'CodeSnippet' plugin in your admin.

    P.S.:

    1. For 3., you can also copy this entire "site-packages/ckeditor" folder to your PROJECT_DIR, and make the replacement.
    2. To my experimentation, adding or removing plugins or making changes to the config files in "YOUR_PROJECT_DIR/static/ckeditor" does not show any effects, even if you remove the entire directory.
    3. Thus I guess there are still some settings we didn't make right, like STATIC_URL, STATIC_ROOT or something. I haven't figured out why since I am a beginner too and I didn't see what is wrong with your settings. I'll try to figure out the root cause and amend this answer if final "Solution" is found. Perhaps the package author 'shaunsephton' could easily figure it and lend some help. :D
    0 讨论(0)
  • 2021-01-07 03:17

    The actual cause is that plugin.js is not added by the CKEditor builder. I have no idea why that is, but each plugin's repository does have a plugin.js.

    0 讨论(0)
  • 2021-01-07 03:29

    I fought with this for ages trying to install the plugins and dependencies manually.

    In the end, I packaged up all the plugins I wanted with CKEditor Builder and dropped it into a ckeditor directory in my STATICFILES_DIRS. /static/ckeditor/ckeditor/plugins & .js etc

    I am using CKEditor within https://github.com/django-blog-zinnia/zinnia-wysiwyg-ckeditor so my settings look like.....

    CKEDITOR_UPLOAD_PATH = 'uploads'
    CKEDITOR_IMAGE_BACKEND = 'pillow'
    
    CKEDITOR_CONFIGS = {
    
            'zinnia-content': {
    
                'toolbar': 'Zinnia',
                "extraPlugins":'codesnippet',
                "codeSnippet_theme": "monokai_sublime",
                'skin': 'moono-dark',
    
                'toolbar_Zinnia': [
                    ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord'],
                    ['Undo', 'Redo'],
                    ['Scayt'],
                    ['Link', 'Unlink', 'Anchor'],
                    ['Image', 'Table', 'HorizontalRule', 'SpecialChar'],
                    ['Source'],
                    ['Maximize', 'Resize'],
                    '/',
                    ['Bold', 'Italic', 'Underline', 'Strike',
                     'Subscript', 'Superscript', '-', 'RemoveFormat'],
                    ['NumberedList', 'BulletedList', '-',
                     'Outdent', 'Indent', '-', 'Blockquote'],
                    ['Styles', 'Format'],['CodeSnippet'],
                    '/',
                    ['Smiley', 'About', 'Preview', 'Templates' ],
                ],
            },
        }
    

    So hopefully, without Zinnia, your settings would look like....

        CKEDITOR_CONFIGS = {
            'default': {
                'toolbar': 'Full',
                "extraPlugins":'codesnippet',
                "codeSnippet_theme": "monokai_sublime",
                'skin': 'moono-dark',                 
            },
        }
    
    0 讨论(0)
  • 2021-01-07 03:39

    This is a probably a different cause of the problem than what was original asked about, because it was a few years ago. But a lot of plugins are not working with the latest release of django-ckeditor, version 5.1.0.

    Took me ages to work out what was wrong - and it's just that the latest version does not include all the plugins. If you pip uninstall and the install version 5.0.0, you get the full plugin suite.

    Figure this might help someone who finds this thread.

    0 讨论(0)
提交回复
热议问题