如何使用post_publish信号发布show_placeholder引用的相关页面? [英] How to use post_publish signal to publish a related page referenced by show_placeholder?

查看:132
本文介绍了如何使用post_publish信号发布show_placeholder引用的相关页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 show_placeholder 在我的网站上显示全局内容。例如,我在侧边栏中使用的小部件。我做了与其他问题中给出的建议类似的事情。我在管理员中创建了模板 extra_placeholders.html 和一个页面(返回ID为 extra-placeholders),该页面使用 extra_placeholders.html 来存储占位符。它永远不会显示在前端,但是我可以使用 {%show_placeholder my_placeholder额外的占位符%}

I am using show_placeholder to display "global" content along my site. For example, a Widget that I use in the sidebar. I did something similar to the suggestions given in this other question. I created a template extra_placeholders.html and a page (with return id "extra-placeholders") in the admin that use extra_placeholders.html to store the placeholders. It is never displayed in the front end but I can access the placeholder of it with {% show_placeholder "my_placeholder" "extra-placeholders" %}

它可以解决我需要解决的一个问题。假设我使用Django-CMS的前端编辑模式来编辑占位符。在某种意义上说,我可以修改内容并保存它。

It works fine with one caveat that I need to solve. Let's say I use the front-end "Edition Mode" of Django-CMS to edit the placeholder. It works in the sense that I can modify the content and save it.

但是,它不会自动发布

我理解原因。占位符实际上属于另一个页面(额外占位符)。因此,当我单击前端的发布时,将保存我看到的页面,但不保存通过 my_placeholder 关联的页面。

I understand the reason. The placeholder actually belongs to another page ("extra-placeholders"). So when I click "Publish" in the front end I save the page that I am seeing but not the one associated through the my_placeholder.

因此,我想每次保存任何其他页面时都使用一个信号来保存额外占位符页面。我发现有此信号但是如何使用它发布我的额外占位符页面?

So, I would like to use a signal to save the "extra-placeholders" page each time any other page is saved. I found that there is this signal but how do I use it to publish my extra-placeholders page?

推荐答案

您实际上想将信号连接到 CMSPlugin ,而不是 Page

You actually want to connect a signal to CMSPlugin, not Page.

类似这样的东西(未经测试! )可能会起作用:

Something like this (untested!) might work:

def signal_handler(instance, **kwargs):
    if not isinstance(instance, CMSPlugin):
        return
    page = Page.objects.get(reverse_id='extra-placeholders', publisher_is_draft = True)
    if page.placeholders.filter(pk=instance.placeholder_id).exists():
        page.publish()

post_save.connect(signal_handler)

这篇关于如何使用post_publish信号发布show_placeholder引用的相关页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆