Python开发Django框架后台admin模板页面使用Kindeditor遇到的一些问题及解决方法:1、post请求上传图片失败 2、样式冲突

17 篇文章 1 订阅

Python开发Django框架后台admin模板页面使用Kindeditor遇到的一些问题及解决方法:1、post请求上传图片失败 2、样式冲突

1、post请求上传图片失败

当项目配置文件settings.py中包含如下配置的时

# CSRF防御中间件,增加防御保护以防止跨站伪造请攻击。通过向表单Post请求中添加隐藏表单字段以及对请求进行检查以获取正确的值。
    'django.middleware.csrf.CsrfViewMiddleware',

post请求中需要有 csrfmiddlewaretoken=“{{ csrf_token }}” 或者 {% csrf_token %}
Kindeditor在django框架后台admin模板页面使用,参考以下配置
admins.py

    class Media:
        js = (
            # 这是在后台新闻添加/修改的页面中追加js文件,富文本框,显示新闻内容内容
            '/static/kindeditor/kindeditor-all.js',  # 主js文件
            '/static/kindeditor/lang/zh-CN.js',  # 语言:中文
            '/static/kindeditor/config1.js',  # kindeditor富文本框主配置文件
        )

config1.js

//后台管理员新闻添加/修改页面,新闻内容富文本框开始
KindEditor.ready(function(K){
    var editor = K.create('#id_content', {
        loadStyleModetrue: false,
        width: '80%',
        height: '400px',
        resizeMode: 1, //编辑器只能调整高度
        allowPreviewEmoticons: true,
        allowImageUpload: true,//是否允许本地上传
        uploadJson: '/kindeditorUploadImages',//上传图片请求地址
        //readonlyMode: true,//只读模式
        items: [
                'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'cut', 'copy', 'paste',
                'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
                'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
                'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen',
                'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
                'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|',
                'emoticons', 'image', '|', 'about'
        ]
    });
});
//富文本框结束

以上配置会出现缺少csrf参数异常,所以需要修改config1.js文件,添加图片上传post请求的csrf参数

//后台管理员新闻添加/修改页面,新闻内容富文本框开始
KindEditor.ready(function(K){
    var editor = K.create('#id_content', {
        loadStyleModetrue: false,
        width: '80%',
        height: '400px',
        resizeMode: 1, //编辑器只能调整高度
        allowPreviewEmoticons: true,
        allowImageUpload: true,//是否允许本地上传
        uploadJson: '/kindeditorUploadImages',//上传图片请求地址
        extraFileUploadParams: {
            'csrfmiddlewaretoken': $("input[name='csrfmiddlewaretoken'][type='hidden']").val() //额外参数
        },
        //readonlyMode: true,//只读模式
        items: [
                'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'cut', 'copy', 'paste',
                'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
                'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
                'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen',
                'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
                'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|',
                'emoticons', 'image', '|', 'about'
        ]
    });
});
//富文本框结束

2、样式冲突

在这里插入图片描述
这是因为Django框架后台admin模板页面样式与Kindeditor样式冲突,可通过在网页中审查元素确定影响元素的css样式,并重新定义css样式
admins.py

    class Media:
        css = {
            'all': ('/static/kindeditor/config-css.css', )  # simple-ui样式与kindeditor样式有部分冲突,重新定义
        }
        js = (
            # 这是在后台新闻添加/修改的页面中追加js文件,富文本框,显示新闻内容内容
            '/static/kindeditor/kindeditor-all.js',  # 主js文件
            '/static/kindeditor/lang/zh-CN.js',  # 语言:中文
            '/static/kindeditor/config1.js',  # kindeditor富文本框主配置文件
        )

config-css.css

.ke-dialog-content .ke-button {
     padding: 0 14px 0 12px;
     color: #333;
}

.ke-dialog-content .ke-button:hover {
     background: url(./themes/default/background.png) no-repeat;
}

.ke-dialog-content .ke-button-common {
     background: url(./themes/default/background.png) no-repeat;
}

.ke-dialog-content ul li{
    list-style-type: unset;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linge511873822

亲的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值