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

  操作系统为OS X 10.9.2,Django为1.6.5.

  1.下载和安装

  1.1 安装 ckeditor

  下载地址 https://github.com/shaunsephton/django-ckeditor ,下载后进入目录安装

django-ckeditor-master bamboo$ sudo python setup.py install

   1.2 安装 Pillow

django-ckeditor-master bamboo$ easy_install Pillow
sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future easy_install Pillow

  2.在Django中进行配置

  setting.py 文件

  添加 ckeditor 到 INSTALLED_APPS 中

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'aaa', # 添加应用aaa
    'bob', # 添加应用bbb
    'ckeditor', # 添加应用富文本编辑器 ckeditor
)

  static 和 media 的路径设置

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static') 

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
CKEDITOR_UPLOAD_PATH = os.path.join(MEDIA_ROOT, 'ckeditor/uploads')

  CHEDITOR_UPLOAD_PATH 指定通过ckeditor所上传的文件的存放目录。

  2.1 在admin中看到编辑页面

  urls.py 文件中,设置ckeditor的url

url(r'^ckeditor/', include('ckeditor.urls')), # 引入ckeditor

  models.py 文件中

from django.db import models
from ckeditor.fields import RichTextField

class Article(models.Model):
    '''日志'''
    title = models.CharField(verbose_name='标题', max_length=150, blank=False, null=False)
    content = RichTextField('正文') # 使用ckeditor中的RichTextField

  配置完成后,即可在admin的页面中看到富文本编辑器的界面

  2.2 在前台页面中看到ckeditor界面

  将安装目录中的ckeditor 复制到static

static bamboo$ cp -ri /Library/Python/2.7/site-packages/django_ckeditor_updated-4.2.8-py2.7.egg/ckeditor/static/* /Users/workspace/myblog/static/

  在 write.html页面中添加JS

<script type="text/javascript" src="/static/ckeditor/ckeditor/ckeditor.js"></script>

  在显示页面view.html中添加 safe 标签

{{ article.content|safe }}

 

  参考资料:

  基于django的博客系统如何完美地使用富文本编辑器ckeditor?    http://www.nanerbang.com/article/2/

  关于在django下使用ckeditor的步骤和技巧 http://blog.163.com/zhulp0372@yeah/blog/static/115894479201172422439697/

 

  

posted @ 2014-08-11 15:56  zifenger  阅读(3813)  评论(1编辑  收藏  举报