从编辑模式html模板发布django-cms插件时,不会渲染模型对象。但是当切换到编辑模式时内容会重新出现

时间:2019-06-20 17:38:40

标签: python django django-cms

实际上,我是Django-cms世界的新手。我几乎尝试过所有相同问题的重复实验,但都无济于事。我仅使用一个ManyToManyField()创建了自定义插件。

我经历了Django-docs,以了解与其他对象的多对多或外键关系,但这并没有帮助。也许我迷失了某个地方,我将非常感谢您的帮助。谢谢。

Try1插件模型:

class PortfolioPluginModel(CMSPlugin):
    portfolio = models.ManyToManyField(Portfolio)

Try2 PluginModel:

class PortfolioPluginModel(CMSPlugin):
    portfolio = models.ManyToManyField(Portfolio)

    def copy_relations(self, oldinstance):
        for p in oldinstance.portfolio.all():
            p.pk = None
            p.plugin = self
            p.save()

Try3 PluginModel:

class PortfolioPluginModel(CMSPlugin):
    portfolio = models.ManyToManyField(Portfolio)

    def copy_relations(self, oldinstance):
        self.portfolios = oldinstance.portfolios.all()

应用模型:

class Portfolio(models.Model):
    author = models.CharField(max_length = 100)
    description = models.TextField()
    image = models.ImageField(upload_to ='portfolioImage',blank = True, null = True)
    published_at = models.DateTimeField(auto_now_add = True)

cms_plugins.py

@plugin_pool.register_plugin  # register the plugin
class PortfolioPluginPublisher(CMSPluginBase):
    model = PortfolioPluginModel  # model where plugin data are saved
    # model = Portfolio(CMSPlugin)
    module = _("Portfolio")
    name = _("Portfolio Plugin")  # name of the plugin in the interface
    render_template = "portfolio_cms_integration/portfolio_plugin.html"
    cache = False

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

portfolio_plugin.html

<div class="filters-content">
            <div class="row grid">
                {% for p in instance.portfolio.all %}
                <div class="single-portfolio col-sm-4 all vector">
                    <div class="relative">
                        <div class="thumb">
                            <div class="overlay overlay-bg"></div>
                            <img class="image img-fluid" src="{{ p.image.url }}" alt="" style="width:399px; height: 400px;">
                        </div>
                        <a href="{{ p.image.url }}" class="img-pop-up">
                            <div class="middle">
                                <div class="text align-self-center d-flex"><img src="{% static 'img/preview.png' %}"
                                        alt=""></div>
                            </div>
                        </a>
                    </div>
                    <div class="p-inner">
                        <h4>{{ p.author }}</h4>
                        <div class="cat">{{ p.description }}</div>
                    </div>
                </div>
                {% endfor %}
        </div>

预期输出:When I publish my post I should see my model objects of portfolio app 实际输出:When I publish my post I dont see any model objects of portfolio app

1 个答案:

答案 0 :(得分:0)

您上一次尝试的效果很好,您只需要删除插件并再次添加即可。这次应使用copy_relations函数。然后,您应该在发布的视图中看到您的插件。

如果您在CMSPlugin模型中具有ForeignKeys或ManyToManyField,请不要忘记copy_relations。 有关更多信息,请查看有关handling relations的Django CMS文档。