前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >django-ckeditor本地图片上传功能

django-ckeditor本地图片上传功能

作者头像
治电小白菜
发布2020-08-25 14:48:04
1.2K0
发布2020-08-25 14:48:04
举报
文章被收录于专栏:技术综合技术综合

django-ckeditor 默认是把本地图片上传给关闭的,需要自己手动操作开启 demo代码地址:https://github.com/klren0312/djangoCKEditor_Stu 初始化操作,记得走一波,同步数据库,创建admin账号等。。。

1.安装ckeditor

代码语言:javascript
复制
pip install django-ckeditor

2.在setting.py中的INSTALLED_APPS中加入两个

代码语言:javascript
复制
INSTALLED_APPS = [
    'ckeditor',
    'ckeditor_uploader'
]

3.在setting.py中设置ckeditor

代码语言:javascript
复制
    MEDIA_URL = "/media/" 
    MEDIA_ROOT = os.path.join(BASE_DIR,"media")
    CKEDITOR_UPLOAD_PATH = "uploads/"
    CKEDITOR_IMAGE_BACKEND = 'pillow'
配置功能项和样式
代码语言:javascript
复制
CKEDITOR_CONFIGS = {
    'default': {
        'update': ['Image', 'Update', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],
        'skin': 'moono',
        'toolbar_Basic': [
            ['Source', '-', 'Bold', 'Italic']
        ],
        'toolbar_YourCustomToolbarConfig': [
            {'name': 'document', 'items': ['Source', '-', 'Save', 'NewPage', 'Preview', 'Print', '-', 'Templates']},
            {'name': 'clipboard', 'items': ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo']},
            {'name': 'editing', 'items': ['Find', 'Replace', '-', 'SelectAll']},
            {'name': 'forms',
             'items': ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton',
                       'HiddenField']},
            '/',
            {'name': 'basicstyles',
             'items': ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat']},
            {'name': 'paragraph',
             'items': ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-',
                       'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl',
                       'Language']},
            {'name': 'links', 'items': ['Link', 'Unlink', 'Anchor']},
            {'name': 'insert',
             'items': ['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak', 'Iframe']},
            {'name': 'styles', 'items': ['Styles', 'Format', 'Font', 'FontSize']},
            {'name': 'colors', 'items': ['TextColor', 'BGColor']},
            {'name': 'tools', 'items': ['Maximize', 'ShowBlocks']},
            {'name': 'yourcustomtools', 'items': [
                # 自定义控件
                'Preview',
                'Maximize',
            ]},
        ],
        'toolbar': 'YourCustomToolbarConfig',  # put selected toolbar config here
        'tabSpaces': 4,
        'extraPlugins': ','.join(
            [
                # your extra plugins here
                'div',
                'autolink',
                'autoembed',
                'embedsemantic',
                'autogrow',
                # 'devtools',
                'widget',
                'lineutils',
                'clipboard',
                'dialog',
                'dialogui',
                'elementspath'
            ]),
    }
}

4.设置model.py

代码语言:javascript
复制
    from ckeditor_uploader.fields import RichTextUploadingField
    class Article(models.Model):
        content = RichTextUploadingField('正文')

5.设置urls.py

代码语言:javascript
复制
    from django.conf.urls import url,include
    urlpatterns = [
        url(r'^ckeditor/', include('ckeditor_uploader.urls'))
    ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

6.运行测试

1.登录进入后台

2.进入文章编辑界面

3.可以看到又上传选项

4.点击浏览,选择图片

5.然后点击 传送到服务器

6.然后会跳转到这个页面,点击确定即可(本地测试是英文。。。)

7.可以看到图片再文章中显示

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.安装ckeditor
  • 2.在setting.py中的INSTALLED_APPS中加入两个
  • 3.在setting.py中设置ckeditor
  • 4.设置model.py
  • 5.设置urls.py
  • 6.运行测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档