...

How to Create a Custom Plugin on DjangoCMS

DjangoCMS custom plugin documentation

DjangoCMS is a powerful content management system that allows developers to create custom plugins for adding dynamic features to their websites. This guide will walk you through the step-by-step process of building a custom DjangoCMS plugin to enhance your website’s functionality.


Step-by-step infographic: Create a Custom DjangoCMS Plugin
The process at a glance

Why Create a Custom Plugin in DjangoCMS?

By default, DjangoCMS provides various built-in plugins, but sometimes you may need custom functionality that is not available out of the box. Creating a custom plugin allows you to:

  • Extend DjangoCMS functionality with your unique features.
  • Provide custom content blocks for easier editing.
  • Optimize performance with tailor-made solutions.

Step 1: Install DjangoCMS

Before creating a custom plugin, ensure that DjangoCMS is installed. If you haven’t set it up yet, install it using the following command:

pip install djangocms-installer
djangocms myproject
cd myproject

Once installed, migrate the database and create a superuser:

python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

This will launch your DjangoCMS project, allowing you to start creating plugins.

👉 Learn more about DjangoCMS on Wikipedia.


Step 2: Create a New Plugin App

To create a custom plugin, first, generate a new Django app inside your project:

python manage.py startapp myplugin

Now, register the new app inside INSTALLED_APPS in your settings.py file:

INSTALLED_APPS = [
    ...
    'myplugin',
    'cms',
    'menus',
]

Step 3: Define the Plugin Model

Inside the models.py file of your new app, define your plugin model:

from django.db import models
from cms.models.pluginmodel import CMSPlugin

class MyCustomPlugin(CMSPlugin):
    title = models.CharField(max_length=255, default="Custom Title")
    content = models.TextField()

Run the following commands to apply migrations:

python manage.py makemigrations myplugin
python manage.py migrate myplugin

Step 4: Create the Plugin Class

Now, create a cms_plugins.py file inside your myplugin folder and define your plugin:

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from .models import MyCustomPlugin
from django.utils.translation import ugettext_lazy as _

@plugin_pool.register_plugin
class MyPlugin(CMSPluginBase):
    model = MyCustomPlugin
    name = _("My Custom Plugin")
    render_template = "myplugin/plugin_template.html"

Step 5: Create the Plugin Template

Inside your myplugin/templates/myplugin/ directory, create a file called plugin_template.html and add the following content:

<div class="custom-plugin">
    <h2>{{ instance.title }}</h2>
    <p>{{ instance.content }}</p>
</div>

Step 6: Register the Plugin in DjangoCMS

Restart the development server:

python manage.py runserver

Now, go to the DjangoCMS admin panel and add your custom plugin to any page using the CMS interface. 🎉


DjangoCMS Plugin Project Structure

Before you write more code, it helps to see how the files fit together. A small plugin app usually looks like this:

hello_plugin/
    __init__.py
    models.py          # fields stored in the database
    cms_plugins.py     # the plugin class that DjangoCMS discovers
    templates/
        hello_plugin/
            hello.html     # the HTML output
    migrations/

DjangoCMS looks for a file named exactly cms_plugins.py in every installed app, so do not rename it.

Add Extra Logic With the render Method

The render method lets you pass additional data to the template. In the example below, the plugin adds an uppercase version of a field.

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from django.utils.translation import gettext_lazy as _

from .models import HelloPlugin


@plugin_pool.register_plugin
class HelloPluginPublisher(CMSPluginBase):
    model = HelloPlugin
    name = _("Hello Plugin")
    render_template = "hello_plugin/hello.html"
    cache = False

    def render(self, context, instance, placeholder):
        context = super().render(context, instance, placeholder)
        context["shout"] = instance.greeting.upper()
        return context

How to Test Your DjangoCMS Plugin

  1. Add your app to INSTALLED_APPS in the project settings.
  2. Run python manage.py makemigrations and python manage.py migrate.
  3. Restart the development server.
  4. Open a page in edit mode, switch to Structure mode, and add the plugin to a placeholder.
  5. Fill in the fields, save, and publish the page to see the result.

Common DjangoCMS Plugin Errors and Fixes

  • The plugin does not appear in the list. The app is missing from INSTALLED_APPS, the server was not restarted, or the file is not named cms_plugins.py.
  • TemplateDoesNotExist. Check the path in render_template and make sure that the templates folder is inside the app.
  • Database errors about a missing table. You forgot to create or run the migrations.
  • Changes do not show. Clear the cache, or set cache = False while you develop.

Best Practices for Custom DjangoCMS Plugins

  • Keep each plugin focused on one job, so editors understand it easily.
  • Use clear field labels and help text for non-technical editors.
  • Escape user input in templates, and rely on Django’s built-in protection against cross-site scripting.
  • Write tests for your models and rendering logic.
  • Document the plugin so that other developers can maintain it.

Frequently Asked Questions

Can a DjangoCMS plugin contain other plugins?

Yes. Set allow_children = True and define which child plugins are allowed. This is useful for containers such as columns or tabs.

Add Fields That Editors Can Control

Real plugins usually need more than one text field. In your model, you can add an image, a link, a choice list, or a rich text field, and DjangoCMS will show them in the plugin form. Keep the form short, because editors work faster when they see only the fields that matter. Add help text to explain each option, and set sensible default values so that a new plugin looks good without any changes.

Finally, commit your plugin to version control, add a short README that explains the fields and the template, and test it on a copy of the site before you use it on the live website. Good documentation makes the plugin easy to reuse in future projects.

If you also work with WordPress, you can compare this approach with our guide on how to create a custom plugin for WordPress.

Final Thoughts: Need Help with DjangoCMS Plugins?

Creating a custom plugin in DjangoCMS allows you to add unique features to your website. However, if you find the process too complex or need a custom solution tailored to your needs, I can help! I offer professional DjangoCMS development services to ensure your website is built efficiently. Contact me today to get started!

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.