django 中的 CK 编辑器无法在生产环境中工作

问题描述 投票:0回答:3

CK-editor 在开发中工作正常,但在生产中却不起作用。

在控制台选项卡中显示错误:

Failed to load resource: the server responded with a status of 404 (Not Found) ckeditor.js

django ckeditor
3个回答
2
投票

我遇到了类似的问题。一切都在本地主机上运行良好,但在我将代码部署到实时服务器后,django-ckeditor 无法工作。当我检查控制台时,django-ckeditor 配置为检查 /static/ckeditor/ckeditor/ckeditor.js。但是在配置 ckeditor 并第一次运行 python manage.py collecstatic 后,ckeditor 配置为将静态文件复制到 STATIC_ROOT 指定的静态文件位置,下面是我的静态文件设置,我相信几乎每个人都这样做:

STATIC_URL = 'static/'
STATICFILES_DIRS = [
    BASE_DIR / 'static'
]
STATIC_ROOT = BASE_DIR / 'staticfiles'

看上面的设置,这里有冲突。 ckedtor 静态文件将被复制到 /staticfiles/ckeditor/ckeditor/ckeditor.js 但 Django 将查看 /static/ckeditor/ckeditor/ckeditor.js。

我所做的简单破解是将所有 ckeditor 文件复制到静态文件夹中,提交更改,然后将更改拉入我的服务器,一切开始正常工作。

如果您还没有使用白噪声,则不需要白噪声来使其工作。


0
投票

发生这种情况是因为您的静态文件处理错误或未处理部署。

确保您的

setting.py
看起来像这样:

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'YOUR STATIC FILE FOLDERS')]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

如果您在

Nginx or 
apache2
please make sure you configured the
.conf` 文件上正确运行应用程序以服务器静态文件。

还有一个简单易行的解决方案,那就是

whitenoise

如果您不想使用 Nginx 或类似服务器提供静态文件。

你可以试试

whitenoise

要使用whitenoise,请先安装它并更新您的settings.py文件,如下所示:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

并将其添加到会话中间件之前的中间件中

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', # Serve static in production without nginx or apache
    'django.contrib.sessions.middleware.SessionMiddleware',
    ........
]

希望该解决方案能够解决您的ckeditor问题

这里是白噪声文档:http://whitenoise.evans.io/en/stable/


0
投票

collectstatic 对我有用。

#command to collect static files
python3 manage.py collectstatic
© www.soinside.com 2019 - 2024. All rights reserved.