Django CMS插件:无法在界面中看到模型字段

时间:2017-02-11 11:12:52

标签: python django django-cms

这是我第一次尝试制作Django CMS插件。我准备好以下文件:

cms_plugins.py

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from cms.models import CMSPlugin

from . import models


class SurveyPluginPublisher(CMSPluginBase):
    """Show Polls entered by Admin."""

    cache = False
    # model = models.QuickAidPluginModel
    module = "Survey"
    name = "Awesome Survey v1.0"
    render_template = 'survey/_hello.html'

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


plugin_pool.register_plugin(SurveyPluginPublisher)

models.py

# encoding: utf-8
from cms.models import CMSPlugin, python_2_unicode_compatible
from django.db import models
from django.core.exceptions import ValidationError
from cms.models import CMSPlugin


class Survey(models.Model):
    name = models.CharField(max_length=400)
    description = models.TextField()

    def __unicode__(self):
        return (self.name)

    def questions(self):
        if self.pk:
            return Question.objects.filter(survey=self.pk)
        else:
            return None

@python_2_unicode_compatible
class SurveyPluginModel(CMSPlugin):
    name = models.CharField("Survey Name", max_length=255, default='Survey Name',
                            help_text='Enter Survey Name')
    description = models.CharField("Survey Description", max_length=500, blank=True, help_text='Write Description here')

    def __str__(self):
        return "Returning some Survey Text"

模板文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h2>Hi Survey</h2>
</body>
</html

但是当我获得编辑页面选项并尝试添加插件时,它会显示此屏幕

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试声明表单:

forms.py:

class SurveyForm(forms.ModelForm):
    class Meta:
        model = Survey
        field = ['name', 'description']

models.py:

# encoding: utf-8
from cms.models import CMSPlugin, python_2_unicode_compatible
from django.db import models
from django.core.exceptions import ValidationError
from cms.models import CMSPlugin


class Survey(models.Model):
    name = models.CharField(max_length=400)
    description = models.TextField()

    def __unicode__(self):
        return (self.name)

    def questions(self):
        if self.pk:
            return Question.objects.filter(survey=self.pk)
        else:
            return None

@python_2_unicode_compatible
class SurveyPluginModel(CMSPlugin):
    name = models.CharField("Survey Name", max_length=255, default='Survey Name',
                        help_text='Enter Survey Name')
    description = models.CharField("Survey Description", max_length=500, blank=True, help_text='Write Description here')
    form = SurveyForm

    def __str__(self):
        return "Returning some Survey Text"

答案 1 :(得分:0)

尝试在model = models.SurveyPluginModel添加一行SurveyPluginPublisher。它需要了解它的模型。

另外,我建议添加fieldsets作为属性。它允许设计管理界面。但是,没有必要。