简体   繁体   English

无法让CKEditor插件在Django中工作

[英]Can't get CKEditor plugins to work in django

I am trying to get the CKEditor plugin, codesnippet, to work in the django admin but am unable to. 我正在尝试让CKEditor插件codenippet在django admin中工作,但无法运行。 CKEditor works if I don't define any CKEDIT_CONFIGS in my settings.py. 如果我在settings.py中未定义任何CKEDIT_CONFIGS,则CKEditor可以工作。 It also works if take out the "extraPlugins" line (and it successfully will adjust the height and width as defined in the CKEDITOR_CONFIGS section). 如果取出“ extraPlugins”行,它也将起作用(成功地将按照CKEDITOR_CONFIGS部分中的定义调​​整高度和宽度)。

I installed CKEditor using the instructions here: https://github.com/shaunsephton/django-ckeditor 我按照这里的说明安装了CKEditor: https : //github.com/shaunsephton/django-ckeditor

CKeditor is located in /static/ckeditor and codesnippet is in /static/ckeditor/plugins/ CKeditor位于/ static / ckeditor中,而codenippet位于/ static / ckeditor / plugins /中

In my settings.py 在我的settings.py中

CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_JQUERY_URL = '/static/js/jquery-2.1.1.min.js'
CKEDITOR_CONFIGS = {
       'default': {
           'toolbar': 'Full',
           'height': 400,
           'width': 900,
           'removePlugins': 'stylesheetparser',
           'extraPlugins': 'codesnippet',
       },
   }

My admin.py 我的管理员

from django.contrib import admin
from blog.models import Article, Category
from django.utils import text
from django import forms
from ckeditor.widgets import CKEditorWidget

class ArticleAdminForm(forms.ModelForm):
    body = forms.CharField(widget=CKEditorWidget())
    class Meta:
        model = Article

I have also tried using just 'plugins' instead of 'extraPlugins' (although this not recomended), but get the same result (which is it breaks CKEditor and the filed doesn't display at all in the admin). 我也尝试过仅使用“插件”而不是“ extraPlugins”(尽管不建议这样做),但得到的结果相同(这会打断CKEditor,并且文件在管理员中根本不会显示)。

Thanks in advance for your help! 在此先感谢您的帮助!

EDIT 11/26/14 编辑11/26/14

OK so this still isn't working. 确定,因此仍然无法正常工作。 I am pretty sure the problem is this (from the nginx error log) 我很确定问题是这样的(来自nginx错误日志)

2014/11/26 14:07:20 [error] 3265#0: *1 open() "/srv/www/mysite/static//ckeditor/ckeditor/plugins/codesnippet/plugin.js" failed

That path isn't right (erroneous double black slash and an extra "ckeditor" directory"). 该路径不正确(错误的双黑斜杠和额外的“ ckeditor”目录”)。

My settings.py now looks like this. 我的settings.py现在看起来像这样。

CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_JQUERY_URL = '/static/js/jquery-2.1.1.min.js'

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

The Extra Plugins line is what is causing the nginx error, I have no idea where it is getting that path. Extra Plugins行是导致nginx错误的原因,我不知道它在哪里获取该路径。

Below is more further information that may be helpful. 以下是更多有用的信息。

This is my full admin.py file 这是我的完整admin.py文件

from django.contrib import admin
from blog.models import Article, Category
from django.utils import text
from django import forms
from django.db import models
from ckeditor.widgets import CKEditorWidget

class ArticleAdminForm(forms.ModelForm):
    body = forms.CharField(widget=CKEditorWidget())

class Meta:
    model = Article

class ArticleAdmin(admin.ModelAdmin):
    form = ArticleAdminForm

admin.site.register(Article, ArticleAdmin)
admin.site.register(Category)

I have copied the following lines into my ckedit.js file and dragged over the appropriate folders in the the plugins folder. 我已将以下行复制到我的ckedit.js文件中,并将其拖到plugins文件夹中的相应文件夹上。

config.extraPlugins = 'dialog';
config.extraPlugins = 'widget';
config.extraPlugins = 'dialogui';
config.extraPlugins = 'lineutils';
config.extraPlugins = 'clipboard';
config.extraPlugins = 'codesnippet';
config.toolbar_Full.push(['codesnippet']);

EDIT 12/1/14 编辑14/1/14

Nginx config file (as requested) Nginx配置文件(根据要求)

server {
    listen 8080;
    server_name mysite.com;
    access_log /srv/www/mysite/logs/access-dev.log;
    error_log /srv/www/mysite/logs/error-dev.log;
    charset utf-8;

    #Django admin css
    location /static/admin {
    alias /srv/www/mysite/static/admin;
    }

    #Django static files
    location /static {
    alias /srv/www/mysite/static/;
    }

    #Django media files
    location /media {
    alias /srv/www/mysite/media/;
    }

    #Uwsgi handles all other requests
    location / {
    auth_basic "Restricted";
    auth_basic_user_file /srv/www/mysite/.nginxpwd;
    uwsgi_pass unix:/var/uwsgi/uwsgi_at-dev.sock;
    include uwsgi_params;
    }
}

EDIT 12/3/14 编辑14/3/14

Full nginx error: 完整的Nginx错误:

2014/11/26 14:36:16 [error] 3461#0: *1 open() "/srv/www/mysite/static//ckeditor/ckeditor/plugins/codesnippet/plugin.js" failed (2: No such file or directory), client: 71.235.164.91, server: 104.131.36.141,, request: "GET /static/ckeditor/ckeditor/plugins/codesnippet/plugin.js?t=E7KD HTTP/1.1", host: "mysite:8080", referrer: "http://mysite:8080/admin/blog/article/3/"

In my settings.py 在我的settings.py中

MEDIA_ROOT = '/srv/www/mysite/media/'
MEDIA_URL = '/media/'

STATIC_ROOT = '/srv/www/mysite/static'
STATIC_URL = '/static/'

CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_JQUERY_URL = '/static/js/jquery-2.1.1.min.js'

The Code Snippet plugin has various dependencies each of which has sub-dependencies, ie: 代码片段插件具有各种依赖性,每个依赖性都有子依赖性,即:

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 / plugins路径中添加代码片段,小部件和Line Utils才能使其正常工作,并使用以下设置来使该按钮显示在工具栏中。

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. 因此,一旦所有插件依赖项都安装完毕,它应该可以工作。

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". 您也已经注意到,它会尝试读取此“ static / ckeditor / ckeditor / plugins / codesnippet / plugin.js” javascript,但即使将插件放在“ YOUR_PROJECT_DIR / static / CKEditor的/ CKEditor的/插件”。 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. 原因是django-ckeditor不在项目目录中搜索静态目录,而是在站点包中其自身路径中搜索自己的静态目录。 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. 使用您的插件(如CodeSnippet之类的额外插件)使用其Builder来构建CKEditor,并使用从CKEditor网站上单独下载的独立版本替换CodeSnippet插件及其依赖项。 (The plugins do not have plugin.js file in their folder) (插件的文件夹中没有plugin.js文件)
  2. Download it and unzip it, you will have a folder named 'ckeditor' with 'lang', 'plugins' as subfolders 下载并解压缩,您将拥有一个名为“ ckeditor”的文件夹,其中带有“ lang”,“ plugins”作为子文件夹
  3. Replace the entire 'ckeditor' directory in 'static/ckeditor/ckeditor' in the folder of 'ckeditor' in your python's site-package folder. 在python的site-package文件夹中的“ ckeditor”文件夹中的“ static / ckeditor / ckeditor”目录中替换整个“ ckeditor”目录。 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. 例如,您的django-ckeditor安装在“ C:\\ Python27 \\ Lib \\ site-packages”中,您将看到“ ckedior”,将“ static / ckeditor / ckeditor”文件夹替换为内置的ckeditor文件夹。 Or you will have virtualenv or whatever, you may do it in its own site-packages. 否则,您将拥有virtualenv或其他任何东西,您可以在其自己的站点包中进行操作。
  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. 像在问题描述中一样添加“ extraPlugins”设置,然后运行python manage.py runserver,您将在管理员中看到“ CodeSnippet”插件。

PS: PS:

  1. For 3., you can also copy this entire "site-packages/ckeditor" folder to your PROJECT_DIR, and make the replacement. 对于3.,您还可以将整个“ site-packages / ckeditor”文件夹复制到您的PROJECT_DIR,然后进行替换。
  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. 以我的实验,即使删除了整个目录,添加或删除插件或对“ YOUR_PROJECT_DIR / static / ckeditor”中的配置文件进行更改也不会显示任何效果。
  3. Thus I guess there are still some settings we didn't make right, like STATIC_URL, STATIC_ROOT or something. 因此,我想还有一些我们没有正确设置的设置,例如STATIC_URL,STATIC_ROOT等。 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. 程序包作者“ shaunsephton”也许可以轻松地找到它并提供一些帮助。 :D :d

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. 最后,我用CKEditor Builder打包了所有想要的插件,并将其放入STATICFILES_DIRS的ckeditor目录中。 /static/ckeditor/ckeditor/plugins & .js etc / static / ckeditor / ckeditor / plugins和.js等

I am using CKEditor within https://github.com/django-blog-zinnia/zinnia-wysiwyg-ckeditor so my settings look like..... 我在https://github.com/django-blog-zinnia/zinnia-wysiwyg-ckeditor中使用CKEditor,所以我的设置看起来像.....

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.... 因此,希望没有Zinnia,您的设置应该像...

    CKEDITOR_CONFIGS = {
        'default': {
            'toolbar': 'Full',
            "extraPlugins":'codesnippet',
            "codeSnippet_theme": "monokai_sublime",
            'skin': 'moono-dark',                 
        },
    }

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. 但是,许多插件无法与最新版本的django-ckeditor 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. 如果您卸载并安装了5.0.0版本,则可以获得完整的插件套件。

Figure this might help someone who finds this thread. 图这可能会帮助找到此线程的人。

The actual cause is that plugin.js is not added by the CKEditor builder. 实际原因是CKEditor构建器未添加plugin.js。 I have no idea why that is, but each plugin's repository does have a plugin.js. 我不知道为什么,但是每个插件的存储库都有一个plugin.js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM