django中使用ckeditor
普通的富文本编辑
-
安装:pip install django-ckeditor
-
激活应用'ckeditor'
-
然后使用
from ckeditor.fields import RichTextField
content = RichTextField()
加上上传图片
- 安装:
pip install django-ckeditor
pip install pillow
-
在INSTALL_APP中激活:'ckeditor', 'ckeditor_uploader'
-
在settings.py中配置:(完整请参考:https://github.com/django-ckeditor/django-ckeditor)
CKEDITOR_UPLOAD_PATH = 'ckeditor/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
- urls.py文件:
from django.conf.urls import url, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^ckeditor/', include('ckeditor_uploader.urls')),
]
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
- 在models.py文件中的使用。
from ckeditor_uploader.fields import RichTextUploadingField
content = RichTextUploadingField(verbose_name="内容", config_name="other")
注:settings中对文件进行配置
详情请参考:https://github.com/django-ckeditor/django-ckeditor
CKEDITOR_CONFIGS = {
'default': {
'toolbar': 'full',
'extraPlugins': ','.join(['codesnippet', 'uploadimage', 'widget', 'lineutils',]),
},
'other':{
...
}
}