python - 无法使用 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')
你能帮帮我吗
解决方案
为了能够上传图片,试试这个
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.
应该这样做。
推荐阅读
- java - Java、套接字、线程
- html - 使用 SVG 制作逼真的(正弦曲线)标志动画/颤动
- python - os.listdir 返回带有特殊字符的奇怪文件名字符串
- python - 在python中格式化列表中的特定元素
- powershell - 批量隐藏 Invoke-WebRequest
- arrays - 将 []byte 数组转换为 uintptr
- significance - 两组之间变化的显着性检验?
- python - 拟合模型时张量输入形状的问题
- javascript - Switch 语句依赖于 React-Router Route
- java - LRU 缓存:linkedhashmap 和迭代器实现不起作用