在CKeditor中,我执行了RichTextUploadingField和所有操作。
一切正常。
我可以在顶部菜单中看到“图像”按钮。
但这对用户来说太混乱了。
就像单击图像按钮->上传->选择图像->将其发送到服务器。对用户来说太多了。就像我需要在ckeditor中上传非常简单的图片一样。
我想要的是简单的图像上传。意思是,用户友好性就像单击图像按钮,然后拖放或选择图像(浏览)并上传。
我不需要很多东西,例如图像信息,链接和高级。因为它使我的用户感到困惑。
有人可以帮我吗?拜托。
通过我使用django-ckeditor == 5.9.0
答案 0 :(得分:0)
settings.py
CKEditor Django配置:
CKEDITOR_JQUERY_URL = 'https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js'
CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_IMAGE_BACKEND = "pillow"
CKEDITOR_CONFIGS = {
'default': {
'toolbar': 'Custom',
'toolbar_Custom': [
['Bold', 'Italic', 'Underline'],
['Link', 'Unlink'],
['Image'],
],
"filebrowserUploadUrl": "https://mywebsite.com/ckeditor/upload/",
}
}
INSTALLED_APPS = [
...
'ckeditor',
'ckeditor_uploader',
...
]
通过Ajax加载和上传图像的Javascript代码:
function cke_img_upload(input){
var myFormData = new FormData();
myFormData.append('upload', input.files[0]);
$.ajax({
url: 'http://mywebsite.com/ckeditor/upload/',
type: 'POST',
processData: false, // important
contentType: false, // important
dataType : 'json',
data: myFormData,
success: function (success_data) {
ckeditor_field = $('.' + $(input).attr('id').replace('img_','')).prev()
if (ckeditor_field[0]){
CKEDITOR.instances[ckeditor_field.attr('id')].insertHtml(jQuery('<img/>', {src: success_data['url']}).attr('data-cke-saved-src',success_data['url']).prop('outerHTML'));
}
},
error: function (error_data) {
console.log(error_data)
}
});
}
function load_image_in_cke(input){
if (input.files && input.files[0]) {
file_extension = input.files[0].name.substring(input.files[0].name.lastIndexOf('.') + 1).toLowerCase();
if (file_extension == "png" || file_extension == "jpeg" || file_extension == "jpg"){
var reader = new FileReader();
reader.onload = function (e) {
cke_img_upload(input)
}
reader.readAsDataURL(input.files[0]);
}
else{
show_error_toaster('Only jpg/png formats are supported !!');
}
}
}
CKEDITOR.on('instanceReady', function(event) {
var cke_toolbox = $('.' + event.editor.id).find('.cke_toolbox')
cke_toolbox.find('.cke_button__image').closest('.cke_toolbar').remove()
cloned_copy = $('#custom_img_cke').clone().css('display','initial')
cloned_copy.find('input').attr('id','img_'+event.editor.id)
cke_toolbox.append(cloned_copy)
});
将此HTML代码粘贴到您使用CKEditor的文件中的任何位置:
<span id="custom_img_cke" class="cke_toolbar cke_toolbar_last" role="toolbar" style="display:none">
<span class="cke_toolbar_start"></span><span class="cke_toolgroup" role="presentation">
<label style="margin:20%;cursor:pointer;" id="cke_c" class="cke_button cke_button__image cke_button_off" href="javascript:void('Image')" title="Image"
tabindex="-1" hidefocus="true" role="button" aria-labelledby="cke_c_label" aria-describedby="cke_c_description" aria-haspopup="false" aria-disabled="false">
<input type="file" accept="image/jpeg,image/png" onchange="load_image_in_cke(this)" style="display: none; background-color: rgb(235, 235, 235);">
<span class="cke_button_icon cke_button__image_icon"
style="background-image:url('/static/ckeditor/ckeditor/plugins/icons.png?t=JB9C');
background-position:0 -960px;background-size:auto;"> </span>
<span id="cke_c_label" class="cke_button_label cke_button__image_label" aria-hidden="false">Image</span><span id="cke_c_description" class="cke_button_label" aria-hidden="false"></span></label></span><span class="cke_toolbar_end"></span></span>
forms.py
from django import forms
import validators as vals
from ckeditor_uploader.widgets import CKEditorUploadingWidget
class ABCForm(forms.Form):
abc_description = forms.CharField(max_length=10000, validators=[],
strip=True,
required=False,
widget=CKEditorUploadingWidget(attrs={
'data-field': 'abc_description',
"class": "form-control abc_description",
'placeholder': 'ABC Description'}))
urls.py
from django.views.decorators.cache import never_cache
from ckeditor_uploader import views as ckeditor_views
urlpatterns = [
...
url(r'^ckeditor/upload/', ckeditor_views.upload, name='ckeditor_upload'),
url(r'^ckeditor/browse/', never_cache(login_required(ckeditor_views.browse)), name='ckeditor_browse'),
...
]