简体   繁体   English

如何识别django-ckeditor的安装?

[英]How can I get my django-ckeditor installation to recognize?

I have been following the instructions at https://github.com/django-ckeditor/django-ckeditor , and wiped the database to get the RichTextField() to register. 我一直在按照https://github.com/django-ckeditor/django-ckeditor上的说明进行操作,并擦除了数据库以获取RichTextField()进行注册。 My models.py is: 我的models.py是:

#!/usr/bin/python

import settings
from ckeditor.fields import RichTextField
from django.db import models

class Page(models.Model):
    title = models.CharField(max_length = 255)
    body = RichTextField()
    location = models.CharField(max_length = 255, verbose_name = "URL")
    keywords = models.CharField(max_length = 255, blank = True)
    meta_description = models.CharField(max_length = 255, blank = True)
    script = models.TextField(blank = True)
    def __unicode__(self):
        return 'Page "' + self.title + '" at ' + self.location
    class Meta:
        ordering = ['title']

However, ckeditor isn't showing up for the "body" field, and a look at the page source for the admin Page editor betrays no reference to ckeditor. 但是,ckeditor不会出现在“ body”字段中,并且查看管理页面编辑器的页面源代码不会显示对ckeditor的引用。

The JavaScript inclusions are: JavaScript包括:

<title>Add page | Django site admin</title>
<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="/static/admin/css/forms.css" />
<!--[if lte IE 7]><link rel="stylesheet" type="text/css" href="/static/admin/css/ie.css" /><![endif]-->

<script type="text/javascript">window.__admin_media_prefix__ = "/static/admin/";</script>

<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.js"></script>

What can/should I do so that the "body" field of a "Page" has a CKeditor for its widget? 我应该/应该做些什么,才能使“页面”的“正文”字段的窗口小部件具有CKeditor?

I know it's an old question, but it hasn't been answered and it may help others... 我知道这是一个古老的问题,但尚未得到解答,它可能会对其他人有所帮助...

Below are the steps I follow when using Ckeditor on django. 以下是在django上使用Ckeditor时要遵循的步骤。

1.Add 'ckeditor' to installed apps on settings.py 1.将'ckeditor'添加到settings.py上已安装的应用程序

2.Add ckeditor config variables in settings.py. 2.在settings.py中添加ckeditor配置变量。 (upload location and tools) (上传位置和工具)

CKEDITOR_UPLOAD_PATH = MEDIA_ROOT
CKEDITOR_BASEPATH = os.path.join(STATIC_ROOT, "ckeditor/")

CKEDITOR_CONFIGS = {

    'default': {
        'toolbar': [
            ['Undo', 'Redo',
             '-', 'Bold', 'Italic', 'Underline',
             '-', 'Link', 'Unlink', 'Anchor',
             '-', 'Format',
             '-', 'SpellChecker', 'Scayt',
             '-', 'Maximize',
             '-', 'Language',
            ],
        ],
        'height': '100%',
        'width': '100%',
        'toolbarCanCollapse': False,
    },
    'full': {
        'toolbar': [
            ['Undo', 'Redo',
             '-', 'Bold', 'Italic', 'Underline', 'NumberedList', 'BulletedList',
             '-', 'Outdent', 'Indent', 'Blockquote', 'CreateDiv',
             '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock',
             '-', 'TextColor', 'BGColor',
             '-', 'Maximize', 'ShowBlocks',  #'Image' ,
             '-', 'Cut', 'Copy', 'Paste', 'PasteText',
            ],
            ['-', 'SpecialChar',
             '-', 'Source',
            ],
            [
                '-', 'Styles', 'Format', 'Font', 'FontSize'
            ],
            [
                '-', 'BidiLtr', 'BidiRtl'
            ]
        ],
        'width': '100%',
        'height': '600px',
        'toolbarCanCollapse': False,
    },
    'disable': {
        'toolbar': [],
        'width': '100%',
        'height': '600px',
        'toolbarCanCollapse': False,
    },
}

3.Add ckeditor to the models 3.将ckeditor添加到模型中

from django.db import models
from ckeditor.fields import RichTextField

class Article(models.Model):
    content = RichTextField(default="")

4.Finally, the magic happens when you add ckeditor widget in a form: 4.最后,当您以某种形式添加ckeditor小部件时,魔术就发生了:

from django import forms
from django.utils.translation import ugettext_lazy as _
from ckeditor.widgets import CKEditorWidget
from apps.articles.models import Article


class ArticleForm(forms.ModelForm):

    content = forms.CharField(widget=CKEditorWidget(config_name='full'), label=_('Content'))

    class Meta:
        model = Article

Of course, don't forget 当然不要忘了

python manage.py collectstatic

to get all static data to your static dir, and 将所有静态数据获取到您的静态目录,以及

python manage.py makemigrations model_name
python manage.py migrate model_name

to create the databases fields. 创建数据库字段。

One more thing! 还有一件事! Add this to the page head when the form is used in order to load the necessary scripts for ckeditor to work 使用表单时,将其添加到页面标题中,以便加载ckeditor正常工作所需的脚本

{{ form_name.media }}

;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM