django 2 博客上的 ckeditor

ckeditor on django 2 blog

我需要在我的博客中添加 ckeditor。我做到了,但没有用。 我在 INSTALLED_APPS:

中安装并添加了 ckeditor
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main',
    'ckeditor',
]

加入models.py:

from ckeditor.fields import RichTextField

class Post(models.Model):
    ...
    body = RichTextField(blank=True, db_index=True)
    ...

我有这个: 还是仅在 DJANGO 管理中有效?

解决方案是使用 Form

将其用作小部件
Class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['body',]
        widgets = {
            'body': CKEditorWidget(),
        }

并且不要忘记在您的模板中加载 js :

{% load static %}
<script type="text/javascript" src="{% static "ckeditor/ckeditor-init.js" %}"></script>
<script type="text/javascript" src="{% static "ckeditor/ckeditor/ckeditor.js" %}"></script>

来自文档的更多信息:https://django-ckeditor.readthedocs.io/en/latest/#widget