标签:存在 class editor stat install widget des length elf
pip install ckeditor
INSTALLED_APPS中添加
‘ckeditor‘,
修改写入字段的格式
主题相关配置(settings)
CKEDITOR_CONFIGS = {
'default': {
'toolbar': 'full',
'height': 300,
'width': 600,
'tabSpaces': 4,
'extraPlugins': 'codesnippet', # 配置代码插件
}
}
静态文件相关配置(图片)
1 INSTALLED_APPS 中添加
'ckeditor_uploader',
2 文件相关
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
CKEDITOR_UPLOAD_PATH = 'Article_media'
URLconfig
from django.conf.urls import url,include
from django.views.static import serve
urlpatterns = [
...
url(r'^ckeditor/', include('ckeditor_uploader.urls'),),
url(r'^media/(?P<path>.*)', serve, {"document_root": develop.MEDIA_ROOT}),
...
]
或
urlpatterns = [
...
url(r'^ckeditor/', include('ckeditor_uploader.urls'),),
...
] + static(settings.MEDIA_URL, documnets_root = settings.MEDIA_ROOT)
# 重写admin/Xadmin表单,在form添加装饰,而不需要修改对象模型,需要重写clean 方法,将表单拿到的数据替换成需要模型中真正存在的字段。
from ckeditor_uploader.widgets import CKEditorUploadingWidget
class ArticleAdminForm(forms.ModelForm):
# 模型中并不存在 content_ck 只是在表单中存在, 将在clean 中 提换,将页面数据和模型分离开 可以做一些其他操作
content_ck = forms.CharField(widget=CKEditorUploadingWidget(), label='正文', required=False)
# 模型中的真实字段
content = forms.CharField(widget=forms.HiddenInput(), required=False)
class Meta:
model = Article
fields = (
'category', 'tag', 'desc', 'title',
'editor_type', 'content', 'content_ck',
'status'
)
def clean(self):
content = self.cleaned_data.get('content_ck')
if not content:
self.add_error('content_ck', '必填项!')
return
self.cleaned_data['content'] = content
return super().clean()
直接修改模型字段将全部的事情交给admin 虽然省事,不灵活也不利于后期开发维护
from ckeditor.fields import RichTextField
class Article(models.Model):
title = models.CharField(max_length=254, unique=True)
body = RichTextField()
def __str__(self):
return self.title
标签:存在 class editor stat install widget des length elf
原文地址:https://www.cnblogs.com/monkey-code/p/12152502.html