作为django staticfiles应用程序打包的ckeditor。
django_ckeditorfiles的Python项目详细描述
作为django应用程序捆绑的ckeditor。
安装
$ pip install django_ckeditorfiles
设置
将'ckeditorfiles'和'django.contrib.staticfiles'添加到 INSTALLED_APPS。
确认安装
启动django开发服务器(runserver),然后打开 http://localhost:8000/static/ckeditorfiles/samples/index.html。你应该得到 ckeditor示例页面。
ckeditor版本
见static/ckeditorfiles/CHANGES.md。
ckeditor.js
ckeditor的整个源代码都在static/ckeditorfiles/中。这意味着 您可以使用:
{% load staticfiles %} <script type="text/javascript" src="{% static "ckeditorfiles/ckeditor.js" %}"></script>
(如果使用ckeditorwidget,则无需执行此操作)
ckeditorfiles.widgets.ckeditorwidget
ckeditorwidget是django.forms.widgets.Textarea的一个子类。它 自动包含ckeditor.js,并添加:
<script type="text/javascript"> CKEDITOR.replace(id, config); </script>
在文本区域之后。id是文本区域的ID,config是 小部件构造函数的config参数,编码为json。
示例
在您的forms.py或您定义表单的任何位置:
from django import forms from ckeditorfiles.widgets import CKEditorWidget class PageForm(forms.Form): body = forms.CharField(widget=CKEditorWidget())
在呈现表单的模板中(我们假设您在模板上下文中命名表单form):
{{ form.media }} {{ form.as_p }}
注意:form.as_p只是一个例子。小部件还可以与 django脆皮的形式,和form.as_ul。重要的部分是form.media, 因为没有它就无法加载ckeditor javascript。或者,你可以 手动添加ckeditor.js(请参见:ckeditorjs)。
CKEditorWidget的配置参数是 CKEDITOR.replace(...)。见: http://docs.ckeditor.com/#!/api/CKEDITOR-method-replace。
配置示例
带有粗体、斜体和显示源的简单工具栏(在自己的框中显示源):
from django import forms from ckeditorfiles.widgets import CKEditorWidget class PageForm(forms.Form): body = forms.CharField( widget=CKEditorWidget(config={ 'toolbar': [{ 'name': 'basic', 'items': [ 'Bold', 'Italic'] }, { 'name': 'source', 'items': [ 'Source'] }] }) )
更复杂的工具栏,适合简单的编辑器,如注释:
from django import forms from ckeditorfiles.widgets import CKEditorWidget class PageForm(forms.Form): body = forms.CharField( widget=CKEditorWidget(config={ #'contentsCss': settings.STATIC_URL + 'my_theme/ckeditor.css', # CSS for the body (see static/ckeditorfiles/contents.css for the default) 'format_tags': 'p;h4', # Only "normal" and "h4" to avoid large headings in the comments 'toolbar': [{ 'name': 'format', 'items': ['Format'] }, { 'name': 'basic', 'items': ['Bold', 'Italic', '-', 'RemoveFormat'] }, { 'name': 'links', 'items': ['Link', 'Unlink'] }, { 'name': 'listandindent', 'items': ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote'] }, { 'name': 'paste', 'items': ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'] }, { 'name': 'tools', 'items': ['Maximize'] }] }) )
完整的默认工具栏(可作为自己配置的按钮名称来源):
class PageForm(forms.Form): body = forms.CharField( widget=CKEditorWidget(config={ 'toolbar': [ { 'name': 'document', 'groups': [ 'mode', 'document', 'doctools' ], 'items': [ 'Source', '-', 'Save', 'NewPage', 'Preview', 'Print', '-', 'Templates' ] }, { 'name': 'clipboard', 'groups': [ 'clipboard', 'undo' ], 'items': [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ] }, { 'name': 'editing', 'groups': [ 'find', 'selection', 'spellchecker' ], 'items': [ 'Find', 'Replace', '-', 'SelectAll' ] }, { 'name': 'forms', 'items': [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField' ] }, '/', # Linebreak { 'name': 'basicstyles', 'groups': [ 'basicstyles', 'cleanup' ], 'items': [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ] }, { 'name': 'paragraph', 'groups': [ 'list', 'indent', 'blocks', 'align', 'bidi' ], 'items': [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl' ] }, { 'name': 'links', 'items': [ 'Link', 'Unlink', 'Anchor' ] }, { 'name': 'insert', 'items': [ 'Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak', 'Iframe' ] }, '/', # Linebreak { 'name': 'styles', 'items': [ 'Styles', 'Format', 'Font', 'FontSize' ] }, { 'name': 'colors', 'items': [ 'TextColor', 'BGColor' ] }, { 'name': 'tools', 'items': [ 'Maximize', 'ShowBlocks' ] }, { 'name': 'others', 'items': [ '-' ] }, { 'name': 'about', 'items': [ 'About' ] } ], 'toolbarGroups': [ {'name': 'document', 'groups': [ 'mode', 'document', 'doctools' ]}, {'name': 'clipboard', 'groups': [ 'clipboard', 'undo' ]}, {'name': 'editing', 'groups': [ 'find', 'selection', 'spellchecker' ]}, {'name': 'forms'}, '/', {'name': 'basicstyles', 'groups': [ 'basicstyles', 'cleanup' ]}, {'name': 'paragraph', 'groups': [ 'list', 'indent', 'blocks', 'align', 'bidi' ]}, {'name': 'links'}, {'name': 'insert'}, '/', {'name': 'styles'}, {'name': 'colors'}, {'name': 'tools'}, {'name': 'others'}, {'name': 'about'} ] }) )
子类ckeditorwidget
您可以通过 子类化ckeditorwidget并在default_config类中提供默认值 属性:
from ckeditorfiles.widgets import CKEditorWidget class MyCKEditorWidget(CKEditorWidget): default_config = {'toolbar': 'Basic', 'height': '300px'}
default_configclass属性提供可以重写的默认值 使用config参数来初始化 重写MyCKEditorWidget的高度配置,如下所示:
widget = MyCKEditorWidget(config={'height': '100px'})