4. Plugins

In this tutorial we’re going to take a basic Django opinion poll application and integrate it into the CMS.

4.1. Create a plugin model

In the models.py of polls_cms_integration add the following:

  1. from django.db import models
  2. from cms.models import CMSPlugin
  3. from polls.models import Poll
  4. class PollPluginModel(CMSPlugin):
  5. poll = models.ForeignKey(Poll)
  6. def __str__(self):
  7. return self.poll.question

This creates a plugin model class; these all inherit from the cms.models.pluginmodel.CMSPlugin base class.

Note

django CMS plugins inherit from cms.models.pluginmodel.CMSPlugin (or a sub-class thereof) and not models.Model.

Create and run migrations:

  1. python manage.py makemigrations polls_cms_integration
  2. python manage.py migrate polls_cms_integration

4.1.1. The Plugin Class

Now create a new file cms_plugins.py in the same folder your models.py is in. The plugin class is responsible for providing django CMS with the necessary information to render your plugin.

For our poll plugin, we’re going to write the following plugin class:

  1. from cms.plugin_base import CMSPluginBase
  2. from cms.plugin_pool import plugin_pool
  3. from polls_cms_integration.models import PollPluginModel
  4. from django.utils.translation import ugettext as _
  5. @plugin_pool.register_plugin # register the plugin
  6. class PollPluginPublisher(CMSPluginBase):
  7. model = PollPluginModel # model where plugin data are saved
  8. module = _("Polls")
  9. name = _("Poll Plugin") # name of the plugin in the interface
  10. render_template = "polls_cms_integration/poll_plugin.html"
  11. def render(self, context, instance, placeholder):
  12. context.update({'instance': instance})
  13. return context

Note

All plugin classes must inherit from cms.plugin_base.CMSPluginBase and must register themselves with the plugin_pool.

A reasonable convention for plugin naming is:

  • PollPluginModel: the model class
  • PollPluginPublisher: the plugin class

You don’t need to follow this convention, but choose one that makes sense and stick to it.

4.1.2. The template

The render_template attribute in the plugin class is required, and tells the plugin which render_template to use when rendering.

In this case the template needs to be at polls_cms_integration/templates/polls_cms_integration/poll_plugin.html and should look something like this:

  1. <h1>{{ instance.poll.question }}</h1>
  2. <form action="{% url 'polls:vote' instance.poll.id %}" method="post">
  3. {% csrf_token %}
  4. <div class="form-group">
  5. {% for choice in instance.poll.choice_set.all %}
  6. <div class="radio">
  7. <label>
  8. <input type="radio" name="choice" value="{{ choice.id }}">
  9. {{ choice.choice_text }}
  10. </label>
  11. </div>
  12. {% endfor %}
  13. </div>
  14. <input type="submit" value="Vote" />
  15. </form>

4.2. Test the plugin

Now you can restart the runserver (required because you added the new cms_plugins.py file, and visit http://localhost:8000/.

You can now drop the Poll Plugin into any placeholder on any page, just as you would any other plugin.

the 'Poll plugin' in the plugin selector

Next we’ll integrate the Polls application more fully into our django CMS project.