Search code examples
javascriptckeditorjinja2

CKEditor and Jinja templates (console errors)


I'm using CKEditor in my flask app to give users WYSIWYG in my text fields, and I'm loading the script in the base.html jinja template.

However, I get console errors in the templates where I don't have a text field:

[CKEDITOR] Error code: editor-incorrect-element. 
Object { element: "description" }

Is there a workaround for this?

Thank you!!


Solution

  • I solved this by adding an empty block in my base.html instead of the script:

    {% block CKEditor_js %}
    {% endblock CKEditor_js %}
    

    Then I added the script in a block at the bottom of the pages I need the script:

    {% block CKEditor_js %}
        <script src="{{ url_for('static', filename='js/cke.js') }}"></script>
    {% endblock CKEditor_js %}
    

    And finally a very small JS file, in this case cke.js, with just:

    CKEDITOR.replace("description");