kotyy
kotyy

Reputation: 167

Django CMS Custom Plugin doesn't render the template

Running a fresh install of django-cms 2.4.0-RC1, django 1.5.1 and python 2.7. I'm trying to create a very simple custom plugin with a single field. The plugin registers in the admin and works fine. It successfully stores in the database. It's just not rendered in my template.

I have verified the render_template path and also tried using a hardcoded absolute path. I have tried overriding the render method in CMSSelectDegreeLevelPlugin.

Am I overlooking something obvious? I've made very similar plugins before (in different versions of django-cms) and had no trouble.

models.py:

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


class SelectDegreeLevel(CMSPlugin):
    degree_level = models.CharField('Degree Level', max_length=50)

cms-plugins.py

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


class CMSSelectDegreeLevelPlugin(CMSPluginBase):
    model = SelectDegreeLevel
    name = _('Degree Level')
    render_template = "cms/plugins/select_degree_level.html"

plugin_pool.register_plugin(CMSSelectDegreeLevelPlugin)

select_degree_level.html

<h1>static text test {{ instance.degree_level }}</h1>

Upvotes: 3

Views: 1628

Answers (1)

Azd325
Azd325

Reputation: 6140

I think you field is not in the context. I do it normally over this way. Add this function to you CMSSelectDegreeLevelPlugin Class

# Way to decide what comes in the context
def render(self, context, instance, placeholder):
        extra_context = {
            'degree_level': instance. degree_level,
        }
        context.update(extra_context)
        return context

# Simplest Way
def render(self, context, instance, placeholder):
    context['instance'] = instance
    return context

Also you can read more here in docs

Upvotes: 0

Related Questions