You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术咨询:能否使用ckeditor5-react的CKEditor渲染Django CKEditor的RichTextField?

Can I use ckeditor5-react's CKEditor component to render Django CKEditor's RichTextField?

Absolutely—you can absolutely use the CKEditor component from ckeditor5-react to work with Django’s RichTextField, and you’re right about matching field names/IDs being key. The trick is aligning CKEditor 5’s configuration with Django’s native CKEditor (which uses CKEditor 4 by default) to replicate its behavior. Let me break this down step by step:

1. Align CKEditor 5’s Plugins & Toolbar with Django’s Defaults

Django CKEditor comes with a preconfigured toolbar (bold, italic, lists, links, image upload, etc.). To match this in CKEditor 5, you’ll need to import and enable the corresponding plugins, then configure the toolbar to mirror Django’s layout.

2. Match Django’s Field Naming Rules

Django auto-generates field IDs in the format id_<field_name> (e.g., id_content for a field named content). Make sure your React CKEditor component uses this ID and matches the field’s name attribute exactly—this ensures Django’s form processing picks up the data correctly.

3. Handle Initial Data & Form Submission

Pass the existing RichTextField content (HTML) to the CKEditor component as initial data. For form submission, either let the CKEditor component auto-generate a hidden textarea (with the correct name) or sync the editor’s content to your React state for AJAX submissions.

4. Configure Image Upload to Work with Django’s Backend

Django CKEditor has a built-in upload endpoint (default: /ckeditor/upload/). Use CKEditor 5’s SimpleUploadAdapter to point to this endpoint, so image uploads work just like the native Django editor.


Example Implementation

First, Install Dependencies

npm install @ckeditor/ckeditor5-react @ckeditor/ckeditor5-classic

React CKEditor Component

import React, { useState } from 'react';
import { CKEditor } from '@ckeditor/ckeditor5-react';
import ClassicEditor from '@ckeditor/ckeditor5-classic/src/classiceditor';

// Import plugins matching Django CKEditor's default features
import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import List from '@ckeditor/ckeditor5-list/src/list';
import Link from '@ckeditor/ckeditor5-link/src/link';
import ImageUpload from '@ckeditor/ckeditor5-image/src/imageupload';
import SimpleUploadAdapter from '@ckeditor/ckeditor5-upload/src/adapters/simpleuploadadapter';

const DjangoRichTextEditor = ({ initialHtml, fieldName }) => {
  const [editorContent, setEditorContent] = useState(initialHtml);

  return (
    <CKEditor
      editor={ClassicEditor}
      data={editorContent}
      onChange={(event, editor) => {
        setEditorContent(editor.getData());
      }}
      config={{
        plugins: [Essentials, Bold, Italic, List, Link, ImageUpload, SimpleUploadAdapter],
        toolbar: ['bold', 'italic', 'bulletedList', 'numberedList', 'link', 'uploadImage'],
        // Point to Django CKEditor's upload endpoint
        simpleUpload: {
          uploadUrl: '/ckeditor/upload/',
        },
      }}
      // Match Django's field naming rules
      name={fieldName}
      id={`id_${fieldName}`}
    />
  );
};

export default DjangoRichTextEditor;

Django Template Integration

<form method="post" id="rich-text-form">
  {% csrf_token %}
  
  <!-- Other form fields -->
  
  <!-- Container for React CKEditor -->
  <div id="react-ckeditor-root"></div>
  
  <button type="submit">Save Changes</button>
</form>

<script>
  // Render the React component with initial data from Django
  ReactDOM.render(
    React.createElement(DjangoRichTextEditor, {
      initialHtml: "{{ form.content.value|escapejs }}",
      fieldName: "content"
    }),
    document.getElementById('react-ckeditor-root')
  );

  // For traditional form submission: no extra work needed—CKEditor's auto-generated textarea
  // will have the correct name, so Django will process it like any other field.
</script>

Key Notes

  • Add More Features: If your Django CKEditor uses extra tools (like underlines, text alignment, or font styles), just import the corresponding CKEditor 5 plugins and add them to the toolbar config.
  • CSRF Protection: CKEditor 5’s SimpleUploadAdapter automatically reads the Django CSRF cookie, so image uploads will work without extra setup.
  • AJAX Submissions: If you’re submitting the form via AJAX, pull the editorContent state value and include it in your request body under the field’s name (e.g., content).

内容的提问来源于stack exchange,提问作者Anthony Lauly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 16:12:27