【问题标题】:Django CMS Custom Plugin doesn't render the templateDjango CMS 自定义插件不呈现模板
【发布时间】:2013-04-05 07:49:39
【问题描述】:

运行 django-cms 2.4.0-RC1、django 1.5.1 和 python 2.7 的全新安装。我正在尝试使用单个字段创建一个非常简单的自定义插件。该插件在管理员中注册并且工作正常。它成功存储在数据库中。它只是没有在我的模板中呈现。

我已经验证了 render_template 路径并尝试使用硬编码的绝对路径。我已经尝试覆盖 CMSSelectDegreeLevelPlugin 中的渲染方法。

我是否忽略了一些明显的东西?我之前做过非常相似的插件(在不同版本的 django-cms 中)并且没有遇到任何问题。

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>

【问题讨论】:

  • 你让它工作了吗...?
  • 我在自己的全新 Django cms 安装中构建了这个。它有效。你能在你项目的地方展示吗?
  • 你已经尝试过稳定的 2.4 版本的 Django Cms 了吗?

标签: django-cms


【解决方案1】:

我认为您的字段不在上下文中。我通常通过这种方式进行操作。将此函数添加到您的 CMSSelectDegreeLevelPlugin 类

# 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

【讨论】:

  • 我最终感到沮丧并下降到 django-cms 2.3.6 并在添加渲染方法后工作。然而,即使使用渲染方法,它在 2.4.0-RC1 中也不起作用。另请注意,2.4.0 中的 render method isn't required 用于简单插件。
  • 很高兴我在 2.3.6 上帮助了你。是的,我现在也看到了,渲染方法默认是添加实例和占位符。
猜你喜欢
  • 2012-05-26
  • 2013-10-18
  • 1970-01-01
  • 2014-04-12
  • 2018-10-13
  • 2011-06-12
  • 2014-01-12
  • 2012-09-24
  • 2011-11-05
相关资源
最近更新 更多