使用django默认的编辑器感觉功能太少了,所以集成一下富文本编辑器。

1、安装和使用

(1)、安装

  1. pip install django-ckeditor

(2)、注册应用

在django的settings中添加‘ckeditor’的app

(3)、配置models

将需要用到富文本编辑器的字段改成RichTextField

上面三步配置过后是不能上传图片的,下面配置上传图片的功能。

(4)、安装pillow库

  1. pip install pillow

(5)、注册应用

添加'ckeditor_uploader'这个app

(6)、在settings指定图片上传的目录

  1. # media
  2. MEDIA_URL='/media/'
  3. MEDIA_ROOT=os.path.join(BASE_DIR,'media')
  4.  
  5. # 配置ckeditor
  6. CKEDITOR_UPLOAD_PATH='upload/'

(7)、在urls中指定要上传的网址

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. from django.conf import settings
  4. from django.conf.urls.static import static
  5. from . import views
  6.  
  7. urlpatterns = [
  8. path('', views.home, name='home'), # 主页路径
  9. path('admin/', admin.site.urls),
  10. path('ckeditor', include('ckeditor_uploader.urls')), # 配置上传url
  11. path('blog/', include('blog.urls')), # 博客app路径
  12. ]
  13.  
  14. # 设置ckeditor的上传
  15. urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

(8)、配置model

把字段改成RichTextUploadingField字段类型

(9)、官方文档

上面这几步差不多就行了,还需要扩展功能,前往-》官方文档

2、变化的部分:

3、上代码

  1. {# 引用模板 #}
  2. {% extends 'base.html' %}
  3.  
  4. {% load staticfiles %}
  5. {% block header_extends %}
  6. <link rel="stylesheet" href="{% static 'blog/blog.css' %}">
  7. {% endblock %}
  8.  
  9. {# 标题 #}
  10. {% block title %}
  11. {{ blog.title }}
  12. {% endblock %}
  13.  
  14. {# 内容#}
  15. {% block content %}
  16. <div class="container">
  17. <div class="row">
  18. <div class="col-6 offset-1">
  19. <ul class="blog-info-description">
  20. <h3>{{ blog.title }}</h3>
  21. <li>作者:{{ blog.author }}</li>
  22. {# 时间过滤器让时间按照自己需要的格式过滤 #}
  23. <li>发布日期:{{ blog.created_time|date:"Y-m-d H:n:s" }}</li>
  24. <li>分类:
  25. <a href="{% url 'blogs_with_type' blog.blog_type.pk %}">
  26. {{ blog.blog_type }}
  27. </a>
  28. </li>
  29. </ul>
  30. <p class="blog-content">{{ blog.content|safe }}</p>
  31.  
  32. <p>上一篇:
  33. {% if previous_blog %}
  34. <a href="{% url 'blog_detail' previous_blog.pk %}">{{ previous_blog.title }}</a>
  35. {% else %}
  36. <span>没有了</span>
  37. {% endif %}
  38. </p>
  39. <p>下一篇:
  40. {% if next_blog %}
  41. <a href="{% url 'blog_detail' next_blog.pk %}">{{ next_blog.title }}</a>
  42. {% else %}
  43. <span>没有了</span>
  44. {% endif %}
  45. </p>
  46. </div>
  47. </div>
  48. </div>
  49.  
  50. {% endblock %}
  51.  
  52. {% block js %}
  53. <script>
  54. $(".nav-blog").addClass("active").siblings().removeClass("active");
  55. </script>
  56. {% endblock %}

blog_detail.html

  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. from ckeditor_uploader.fields import RichTextUploadingField
  4.  
  5. # Create your models here.
  6.  
  7. # 博客分类
  8. class BlogType(models.Model):
  9. type_name = models.CharField(max_length=15) # 博客分类名称
  10.  
  11. def __str__(self): # 显示标签名
  12. return self.type_name
  13.  
  14. # 博客
  15. class Blog(models.Model):
  16. title = models.CharField(max_length=50) # 博客标题
  17. blog_type = models.ForeignKey(BlogType, on_delete=models.DO_NOTHING) # 博客分类
  18. content = RichTextUploadingField() # 博客内容,使用富文本编辑
  19. author = models.ForeignKey(User, on_delete=models.DO_NOTHING) # 博客作者
  20. created_time = models.DateTimeField(auto_now_add=True) # 博客创建时间
  21. last_updated_time = models.DateTimeField(auto_now=True) # 博客更新事件
  22.  
  23. def __str__(self): # 显示标题名
  24. return "<Blog:{}>".format(self.title)
  25.  
  26. class Meta:
  27. ordering = ['-created_time'] # 定义排序规则,按照创建时间倒序

models.py

  1. """
  2. Django settings for myblog project.
  3.  
  4. Generated by 'django-admin startproject' using Django 2.1.3.
  5.  
  6. For more information on this file, see
  7. https://docs.djangoproject.com/en/2.1/topics/settings/
  8.  
  9. For the full list of settings and their values, see
  10. https://docs.djangoproject.com/en/2.1/ref/settings/
  11. """
  12.  
  13. import os
  14.  
  15. # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
  16. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  17.  
  18. # Quick-start development settings - unsuitable for production
  19. # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
  20.  
  21. # SECURITY WARNING: keep the secret key used in production secret!
  22. SECRET_KEY = 'ea+kzo_5k^6r7micfg@lar1(rfdc08@b4*+w5d11=0mp1p5ngr'
  23.  
  24. # SECURITY WARNING: don't run with debug turned on in production!
  25. DEBUG = True
  26.  
  27. ALLOWED_HOSTS = []
  28.  
  29. # Application definition
  30.  
  31. INSTALLED_APPS = [
  32. 'django.contrib.admin',
  33. 'django.contrib.auth',
  34. 'django.contrib.contenttypes',
  35. 'django.contrib.sessions',
  36. 'django.contrib.messages',
  37. 'django.contrib.staticfiles',
  38. 'blog.apps.BlogConfig', # 将自己创建的app添加到设置中
  39. 'ckeditor',
  40. 'ckeditor_uploader',
  41. ]
  42.  
  43. MIDDLEWARE = [
  44.  
  45. 'django.middleware.security.SecurityMiddleware',
  46. 'django.contrib.sessions.middleware.SessionMiddleware',
  47.  
  48. 'django.middleware.common.CommonMiddleware',
  49. 'django.middleware.csrf.CsrfViewMiddleware',
  50. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  51. 'django.contrib.messages.middleware.MessageMiddleware',
  52. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  53. 'blog.middleware.mymiddleware.My404', # 添加自己的中间件
  54. ]
  55.  
  56. ROOT_URLCONF = 'myblog.urls'
  57.  
  58. TEMPLATES = [
  59. {
  60. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  61. 'DIRS': [
  62. os.path.join(BASE_DIR, 'templates'),
  63. ],
  64. 'APP_DIRS': True,
  65. 'OPTIONS': {
  66. 'context_processors': [
  67. 'django.template.context_processors.debug',
  68. 'django.template.context_processors.request',
  69. 'django.contrib.auth.context_processors.auth',
  70. 'django.contrib.messages.context_processors.messages',
  71. ],
  72. },
  73. },
  74. ]
  75.  
  76. WSGI_APPLICATION = 'myblog.wsgi.application'
  77.  
  78. # Database
  79. # https://docs.djangoproject.com/en/2.1/ref/settings/#databases
  80.  
  81. DATABASES = {
  82. # 'default': {
  83. # 'ENGINE': 'django.db.backends.sqlite3',
  84. # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  85. # }
  86. 'default': {
  87. 'ENGINE': 'django.db.backends.mysql',
  88. 'NAME': 'myblogs', # 要连接的数据库,连接前需要创建好
  89. 'USER': 'root', # 连接数据库的用户名
  90. 'PASSWORD': 'felixwang', # 连接数据库的密码
  91. 'HOST': '127.0.0.1', # 连接主机,默认本级
  92. 'PORT': 3306 # 端口 默认3306
  93. }
  94. }
  95.  
  96. # Password validation
  97. # https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
  98.  
  99. AUTH_PASSWORD_VALIDATORS = [
  100. {
  101. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  102. },
  103. {
  104. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  105. },
  106. {
  107. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  108. },
  109. {
  110. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  111. },
  112. ]
  113.  
  114. # Internationalization
  115. # https://docs.djangoproject.com/en/2.1/topics/i18n/
  116.  
  117. # LANGUAGE_CODE = 'en-us'
  118. # 语言
  119. LANGUAGE_CODE = 'zh-hans'
  120.  
  121. # TIME_ZONE = 'UTC'
  122. # 时区
  123. TIME_ZONE = 'Asia/Shanghai'
  124.  
  125. USE_I18N = True
  126.  
  127. USE_L10N = True
  128.  
  129. # 不考虑时区
  130. USE_TZ = False
  131.  
  132. # Static files (CSS, JavaScript, Images)
  133. # https://docs.djangoproject.com/en/2.1/howto/static-files/
  134.  
  135. STATIC_URL = '/static/'
  136. STATICFILES_DIRS = [
  137. os.path.join(BASE_DIR, "static")
  138. ]
  139.  
  140. # media
  141. MEDIA_URL='/media/'
  142. MEDIA_ROOT=os.path.join(BASE_DIR,'media')
  143.  
  144. # 配置ckeditor
  145. CKEDITOR_UPLOAD_PATH='upload/'
  146.  
  147. # 自定义参数
  148. EACH_PAGE_BLOGS_NUMBER = 7

settings.py

  1. """myblog URL Configuration
  2.  
  3. The `urlpatterns` list routes URLs to views. For more information please see:
  4. https://docs.djangoproject.com/en/2.1/topics/http/urls/
  5. Examples:
  6. Function views
  7. 1. Add an import: from my_app import views
  8. 2. Add a URL to urlpatterns: path('', views.home, name='home')
  9. Class-based views
  10. 1. Add an import: from other_app.views import Home
  11. 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
  12. Including another URLconf
  13. 1. Import the include() function: from django.urls import include, path
  14. 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
  15. """
  16. from django.contrib import admin
  17. from django.urls import path, include
  18. from django.conf import settings
  19. from django.conf.urls.static import static
  20. from . import views
  21.  
  22. urlpatterns = [
  23. path('', views.home, name='home'), # 主页路径
  24. path('admin/', admin.site.urls),
  25. path('ckeditor', include('ckeditor_uploader.urls')), # 配置上传url
  26. path('blog/', include('blog.urls')), # 博客app路径
  27. ]
  28.  
  29. # 设置ckeditor的上传
  30. urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urls.py

搭建自己的博客(十三):为博客后台添加ckeditor富文本编辑器的更多相关文章

  1. springboot中使用kindeditor富文本编辑器实现博客功能

    kindeditor在之前已经用过,现在在springboot项目中使用.并且也在里面使用了图片上传以及回显等功能. 其实主要的功能是图片的处理:kindeditor对输入的内容会作为html标签处理 ...

  2. python 全栈开发,Day83(博客系统子评论,后台管理,富文本编辑器kindeditor,bs4模块)

    一.子评论 必须点击回复,才是子评论!否则是根评论点击回复之后,定位到输入框,同时加入@评论者的用户名 定位输入框 focus focus:获取对象焦点触发事件 先做样式.点击回复之后,定位到输入框, ...

  3. PHP如何搭建百度Ueditor富文本编辑器

    本文为大家分享了PHP搭建百度Ueditor富文本编辑器的方法,供大家参考,具体内容如下 下载UEditor 官网:下载地址 将下载好的文件解压到thinkphp项目中,本文是解压到PUBLIC目录下 ...

  4. 万里长征第二步——django个人博客(第六步 ——添加富文本编辑器)

    下载kindeditor 在admin.py文件中配置链接 class Media: js = ( '/static/js/kindeditor-4.1.10/kindeditor-min.js', ...

  5. [BBS]搭建开源论坛之JForum富文本编辑器更换

    本文作者:sushengmiyan  本文地址:http://blog.csdn.net/sushengmiyan/article/details/47866905 上一节我们将开发环境搭建完成,我们 ...

  6. Django1.9开发博客(11)- 富文本与代码高亮

    TinyMCE是一个轻量级的基于浏览器的所见即所得编辑器,支持目前流行的各种浏览器,由JavaScript写成. 功能配置灵活简单(两行代码就可以将编辑器嵌入网页中),支持AJAX.另一特点是加载速度 ...

  7. Django实现的博客系统中使用富文本编辑器ckeditor

    操作系统为OS X 10.9.2,Django为1.6.5. 1.下载和安装 1.1 安装 ckeditor 下载地址 https://github.com/shaunsephton/django-c ...

  8. 【网站搭建】搭建独立域名博客 -- 独立域名博客上线了 www.hanshuliang.com

    博客是安装在阿里云的服务器上. 小结 : -- 进入数据库命令 :mysql -uroot -p123456 ; -- 检查nginx配置语法 :.../nginx/sbin/nginx -t; -- ...

  9. 搭建独立域名博客 -- 独立域名博客上线了 www.hanshuliang.com

    博客是安装在阿里云的服务器上. 小结 : -- 进入数据库命令 :mysql -uroot -p123456 ; -- 检查nginx配置语法 :.../nginx/sbin/nginx -t; -- ...

随机推荐

  1. SQL SERVER 中如何获取日期(一个月的最后一日、一年的第一日等等)

    https://blog.csdn.net/deepwishly/article/details/9101307 这是计算一个月第一天的SQL 脚本:   SELECT DATEADD(mm, DAT ...

  2. Python查看模块

    1.查看Python所有内置模块 按以下链接打开,每个模块有介绍,可以选择不同的版本 https://docs.python.org/3.6/library/index.html 2.查看Python ...

  3. 【转载】C#使用Split函数根据特定分隔符分割字符串

    在C#程序开发过程中,很多时候可能需要将字符串根据特定的分割字符分割成字符或者List集合,例如根据逗号将字符串分割为数组,或者根据竖线将字符串分割成数组,C#中提供了Split()函数来快速将字符串 ...

  4. python数据统计出海品牌

    当国内市场处于红海之中时,市场全球化已成为大势所趋.越来越多的国产品牌远走高飞,纷纷将品牌拿出来. 2019年,中国品牌十大品牌中,华为品牌力指数同比增长22%,阿里巴巴品牌力指数增长48%,小米品牌 ...

  5. Sass和Compass学习笔记系列之Sass

    最近在慕课网学习Sass和Compass,学习链接地址:https://www.imooc.com/learn/364,现在整理笔记如下: 一.使用Sass和Compass的优点: a.使用Sass和 ...

  6. Python:GeoJson格式的多边形裁剪Tiff影像并计算栅格数值

    JSON是通过键值对表示数据对象的一种格式,其全称为JavaScript Object Notation,它采用完全独立于编程语言的文本格式来存储和表示数据,轻量级.简洁清晰的层次结构.容易解析等特点 ...

  7. 【前端开发】】ES6属性promise封装js动画

    如下是我写的demo源码: 可以直接复制用浏览器打开看到效果哦: <!DOCTYPE html> <html> <head> <meta charset=&q ...

  8. Mycat学习-单独启动mycat

    Mycat下载地址:http://mycat.io/ Mycat安装:解压缩即可.   Mycat作为一个中间件,实现mysql协议,是可以不依赖数据库单独运行的. 对前端应用连接来说就是一个数据库, ...

  9. 【转载】linux如何将新硬盘挂载到home目录下

    举例说明: 新增磁盘的设备文件名为 /dev/vdb 大小为100GB. #fdisk -l 查看新增的的磁盘 1.对新增磁盘进行分区 #fdisk /dev/vdb 按提示操作 p打印 n新增 d ...

  10. Linq 中 Join 的用法

    Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 注:本文内容主要来自<Linq实战>,本例中用到的对象请见文章底部. 1. 组连接 组连接是与分组查询是一样的. ...