安装ckeditor
pip install django-ckeditor
pip install pillow
在settings.py里设置
MEDIA_URL= '/media/'
MEDIA_ROOT= os.path.join(BASE_DIR, 'media')
CKEDITOR_UPLOAD_PATH= "uploads/"
CKEDITOR_JQUERY_URL= 'https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js'
CKEDITOR_IMAGE_BACKEND= 'pillow'
# 这个配置,有很多样式,可以自己去官网找样式。
CKEDITOR_CONFIGS = {
'default': {
'toolbar': 'full',
}
}
并注册app
INSTALLED_APPS= [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'xadmin',
'crispy_forms',
'ckeditor',
'ckeditor_uploader',
]
配置Urls
import xadmin
from django.urlsimport path, include
from django.confimport settings
from django.conf.urls.staticimport static
urlpatterns= [
path('tg_xadmin/', xadmin.site.urls),
path('ckeditor/', include('ckeditor_uploader.urls')),
]
if settings.DEBUG:
urlpatterns+= static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
使用的时候直接在你的models里把textfield改成richtextfield就可以了
from django.db import models
from ckeditor.fields import RichTextField
class Article(models.Model):
content = RichTextField('文章标题')