So far, however, the polls application has been integrated into the project, but not into django CMS
itself.

If you’re already familiar with the CMS for a little, you’ll have
encountered django CMS Plugins - the objects you can place into placeholders on
your pages: “Text”, “Image” and so forth.

We’re now going to extend the Django poll application so we can embed a poll easily into any CMS
page. We’ll put this integration code in a separate package, a Polls/CMS Integration application
in our project.

Note

Why not build the plugin code into the polls application package?

This would certainly be possible, and in fact, if you were developing your own application
it’s what we would recommend. For a third-party application such as Polls however, placing the
plugin code into a separate package means we don’t have to modify or fork the original.

Create a new package at the project root called polls_cms_integration:

pythonmanage.pystartapppolls_cms_integration

So our workspace looks like this:

env/src/# the django polls application is in herepolls_cms_integration/# the newly-created application__init__.pyadmin.pymodels.pymigrations.pytests.pyviews.pymysite/static/project.dbrequirements.txt

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:

fromcms.plugin_baseimportCMSPluginBasefromcms.plugin_poolimportplugin_poolfrompolls_cms_integration.modelsimportPollPluginModelfromdjango.utils.translationimportugettextas_classPollPluginPublisher(CMSPluginBase):model=PollPluginModel# model where plugin data are savedmodule=_("Polls")name=_("Poll Plugin")# name of the plugin in the interfacerender_template="polls_cms_integration/poll_plugin.html"defrender(self,context,instance,placeholder):context.update({'instance':instance})returncontextplugin_pool.register_plugin(PollPluginPublisher)# register the plugin