How to use post_publish signal to publish a related page referenced by show_placeholder?

不羁岁月 提交于 2019-12-11 19:50:00

问题


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" %}

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.

However, it does not get published automatically.

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?


回答1:


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)


来源:https://stackoverflow.com/questions/22575747/how-to-use-post-publish-signal-to-publish-a-related-page-referenced-by-show-plac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!