django 2博客上的ckeditor

时间:2019-01-18 16:55:11

标签: python django ckeditor

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

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)
    ...

我有这个: enter image description here 还是仅在DJANGO ADMIN中起作用?

1 个答案:

答案 0 :(得分:0)

解决方案是使用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