Search code examples
djangodjango-cmsdjango-apps

create plugin from polls application


I installed app Polls from with pip install -e git+http://[email protected]/divio/django-polls.git#egg=pollsfrom. Application is saved /me/env/src/polls/ . I run server from /me/project/. I get an error Poll Plugin cant be imported. How can i define that Polls app use own models.

Now I want to create plugin and placeholder in my template.

cms_plugins.py

    from cms.plugin_base import CMSPluginBase
    from cms.plugin_pool import plugin_pool
    from polls.models import PollPlugin as PollPluginModel
    from django.utils.translation import ugettext as _

    class PollPlugin(CMSPluginBase):
        model = PollPluginModel # <--not sure what to put here.
        name = _("Poll Plugin") # Name of the plugin
        render_template = "polls/plugin.html" # 

        def render(self, context, instance, placeholder):
            context.update({'instance':instance})
            return context

    plugin_pool.register_plugin(PollPlugin) # register the plugin

polls/models.py

class Poll(models.Model):
        question = models.CharField(max_length=200)

        def __unicode__(self):  # Python 3: def __str__(self):
            return self.question


class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __unicode__(self):  # Python 3: def __str__(self):
        return self.choice_text

Solution

  • first you need to add new app.

    Define PollPluginModel and link it with poll polls/models from django.db import models from cms.models import CMSPlugin from polls.models import Poll

    class PollPluginModel(CMSPlugin):
        poll = models.ForeignKey(Poll)
    
        def __unicode__(self):
            return self.poll.question
    

    Please check http://docs.django-cms.org/en/release-3.4.x/introduction/plugins.html .Here is complete tutorial for plugin.