pythondjangotemplatesckeditordjango-ckeditor

how to add custom templates in django-ckeditor in django


I am trying to use the Templates feature of the CKEditor in Django and I want to add some custom templates in the list. I've used this approach. I've tried editing

myvenv\Lib\site-packages\ckeditor\static\ckeditor\ckeditor\plugins\templates\templates\default.js

default.js

CKEDITOR.addTemplates("default",{imagesPath:CKEDITOR.getUrl(CKEDITOR.plugins.getPath("templates")+"templates/images/"),templates:[{title:"Welcome Template",image:"template2.gif",description:"A template that you can send to clients as a welcome message.",html:' <h2>Welcome</h2> <p>Hi, <strong> Name here </strong> </p><p>You have recently shown an interest . . . </p>'},]});

but it didn't help, I've tried the collect static command but it didn't help either. I am still seeing the same default three templates. I'm not sure but there is another option of using extra plugins in the settings.py file but I don't know how to use it for this problem.

settings.py

CKEDITOR_UPLOAD_PATH = "file_upload/"

CKEDITOR_CONFIGS = {
    'default': {
        'toolbar':'full',
    },
}

apps

INSTALLED_APPS = [
...,
'ckeditor',
'ckeditor_uploader',
]

urls.py

urlpatterns = [
path('ckeditor/',include('ckeditor_uploader.urls')),]

models.py

class text(models.Model):
    body = RichTextField(blank=True, default="None")

So my question is how to add custom templates in django-ckeditor==6.3.2?


Solution

  • I haven't used this plugin, but from the documentation it seems you're missing an extra step, which is modifying the template location in the HTML template. From the documentation you can read:

    "sometimes you have to hardcode CKEDITOR_BASEPATH somewhere. It is recommended to override the admin/base_site.html template with your own if you really need to do this, i.e.:"

    {% extends "admin/base_site.html" %}
    {% block extrahead %}
    <script>window.CKEDITOR_BASEPATH = '/static/ckeditor/ckeditor/';</script>
    {{ block.super }}
    {% endblock %}
    

    In that case, make sure the path is accessible from the site. Also, make sure you have added the 'templates' plugin in your CKEditor configuration: documentation.