添加富文本编辑器Ckeditor
使用pip安装 django-ckeditor 和 pillow,pillow用于生成缩略图,用来在编辑器里浏览上传的图片:
pip install django-ckeditor
pip install pillow
修改项目的settings.py文件:
INSTALLED_APPS = [
'ckeditor',
'ckeditor_uploader',
]
设置ckeditor的default格式:
CKEDITOR_CONFIGS = {
'default':{
'language':'zh-cn',
'toolbar':'full',
'height':'150px',
'width':'auto',
'tabSpaces':4, 'extraPlugins':','.join(['codesnippet','uploadimage','widget','lineutils',]),
}
}
CKEDITOR_UPLOAD_PATH = '/static/images/'
CKEDITOR_IMAGE_BACKEND = 'pillow'
修改项目的urls.py文件:
urlpatterns = [
url(r'^ckeditor/',include('ckeditor_uploader.urls')),
]
然后在Apps/indexcal中调用ckeditor,修改Apps/indexcal/models.py:
from django.db import models
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField
from django.contrib.auth.models import User
class Category(models.Model):
label = models.CharField(blank=True, max_length=50)
slug = models.SlugField()
def __str__(self):
return self.label
class Blog(models.Model):
title = models.CharField(verbose_name='博客文章', max_length=50)
category = models.ForeignKey(Category, on_delete=models.CASCADE,)
author = models.ForeignKey(User, on_delete=models.CASCADE,)
content = RichTextField(default='写在这里...', verbose_name='内容')
def __str__(self):
return self.title
修改Apps/indexcal/admin.py,注册定义的类:
from django.contrib import admin
from . import models
# Register your models here.
admin.site.register(models.Category)
admin.site.register(models.Blog)
最后不要忘了迁移数据库:
sudo python3 manage.py makemigrations
sudo python3 python manage.py migrate
如果要部署在服务器上,需要收集静态文件,否则无法显示编辑器:
sudo python3 manage.py collectstatic