0
votes

I want to create a custom plugin for Django CMS. As the guide was showing, I created some examples. But now the goal is to create a plugin that will get the data from (mysql) database. It will load all titles that belong to the menu, because I want to have some similar to table of contents.

To get data from an custom model, the code goes like this:

  • models.py:

    from cms.models.pluginmodel import CMSPlugin
    
    from django.db import models
    
    class Hello(CMSPlugin):
    
        guest_name = models.CharField(max_length=50, default='Guest')
    
  • cms_plugins.py:

from cms.plugin_base import CMSPluginBase

from cms.plugin_pool import plugin_pool

from django.utils.translation import ugettext_lazy as _
from .models import Hello

class HelloPlugin(CMSPluginBase):
    model = Hello
    name = _("Hello Plugin")
    render_template = "hello_plugin.html"
    cache = False

    def render(self, context, instance, placeholder):
        context = super(HelloPlugin, self).render(context, instance, placeholder)
        return context

plugin_pool.register_plugin(HelloPlugin)

But as cms_title belongs to the Django-CMS by default, what options are possible here? Where can I find the definition of CMS model with name Title? Would setting it to CMSPlugin instance be a bad way?

1

1 Answers

0
votes

Well, after few hours of struggling with this case, I finally succeeded to resolve my problem.

First of all, to answer the part of the question with CMS model and parameter title (in db: cms_title). Creating a new model with the FK to CMS title is the right way. In models.py:

class TitlePlugin(CMSPlugin):
        title = models.ForeignKey(Title)

As next, you need to import it to cms_plugins.py, so it will look like this:

from .models import TitlePlugin

 class MyTitlePluginClass(CMSPluginBase):
    model = TitlePlugin
    render_template = "titles.html"
    cache = False

As you see, the model that we load is TitlePlugin, we defined in models.py (which has FK to original cms_title).

And now, render it:

def render(self, context, instance, placeholder):

    context = super(MyTitlePluginClass, self).render(context, instance, placeholder)
    return context

But my goal was to load it into template for the purpose of 'table of contents', right? So I had to change some things.

I removed models.py content (not needed!)

The new cms_plugins.py had modifications too: first:

from cms.models.pagemodel import Page
#we import the Page model

and the renewed class:

class MyTitlePluginClass(CMSPluginBase):
    model = CMSPlugin
    render_template = "titles.html"
    cache = False

def render(self, context, instance, placeholder):
    pages = Page.objects.filter(in_navigation=True, publisher_is_draft=False)

    #context = {
    #'pages' : pages,
    #}
    # edit: append to 'pages' existing list!
    context['pages'] = pages

    context = super(MyTitlePluginClass, self).render(context, instance, placeholder)
    return context

plugin_pool.register_plugin(MyTitlePluginClass)

And in the template, we simply print it using for loop titles.html:

{% for page in pages %}
<div class="panel callout ">
    {{ page }}
</div>
{% endfor %}