首页 > 解决方案 > 无法使用 Django-CKEditor 上传图像(错误 400)

问题描述

当我想在 CKEditor 中上传图像时,它给了我错误“文件上传期间发生 HTTP 错误(错误状态:400)”。

请检查下面的图片

图片

在 urls.py 里面

from django.contrib import admin

from django.urls import path,include
from django.conf import settings
from django.conf.urls import url
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls.static import static


urlpatterns = i18n_patterns(
    ....
    path('ckeditor/',include('ckeditor_uploader.urls')),
    ....
    )



if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

在 settings.py ---> 对于 CKEDITOR

INSTALLED_APPS = [
    ...,
    'ckeditor',
    'ckeditor_uploader',
    ...,
    
    
]

#Ckeditor


CKEDITOR_IMAGE_BACKEND = "pillow"

CKEDITOR_UPLOAD_PATH="uploads/"


CKEDITOR_CONFIGS={

     'myconfig':{
        'toolbar':"Custom",
    'toolbar_Custom':[

    ['Styles','Format','Font','FontSize','BidiLtr','BidiRtl'],
    ['JustifyLeft','JustifyCenter','JustifyRight','NumberedList', 'BulletedList','Bold','Italic','Underline','Strike','Undo','Redo'],
    ['Link','Unlink','Anchor'],
    ['TextColor','BGColor'],
    ['Smilely','SpecialChar'],
    ['Source','Scayt','Maximize'],
    ['Table','Templates','Iframe','Image'],
    ],
    'height': 100,
    },

}

在模型.py

 content = RichTextUploadingField(config_name='myconfig')

你能帮帮我吗

标签: pythondjangockeditor

解决方案


为了能够上传图片,试试这个

pip install django-ckeditor // or just make sure it is installed

在设置中:

INSTALLED_APPS = [
    ...
    'ckeditor',
    'ckeditor_uploader',
],

// at the bottom of the file add

CKEDITOR_UPLOAD_PATH = "uploads/"
CKEDITOR_IMAGE_BACKEND = "pillow"

在主要网址中:

 path('ckeditor/', include('ckeditor_uploader.urls')),

在你的models.py

from ckeditor_uploader.fields import RichTextUploadingField

body = RichTextUploadingField(blank=True, null=True) // body, image, or whatever name  you choose.

应该这样做。


推荐阅读