python - DjangoCMS - 具有共享页面的多个站点

标签 python django django-cms

我正在尝试使用带有一些共享页面的 DjangoCMS 构建多个网站。是否可以创建一个在所有 Django Site 之间共享的页面?

使用基本的 DjangoCMS 配置,当页面在 Site 上发布时,它不会出现在其他 Site 上。我想知道这是否可以任何方式配置。

在查看代码时,我发现 TreeNode 链接到特定的 Site ( https://github.com/divio/django-cms/blob/develop/cms/models/pagemodel.py#L52 ),所以我想如果可能的话,它会获胜就这么简单。


class TreeNode(MP_Node):
    # [...]
    site = models.ForeignKey(
        Site,
        on_delete=models.CASCADE,
        verbose_name=_("site"),
        related_name='djangocms_nodes',
        db_index=True,
    )
    # [...]

如果 DjangoCMS 不处理这个问题,我可以使用外部模块,甚至一些想法或如何处理这个问题的线索,我真的没有线索。

非常感谢!

最佳答案

我已经通过 DjangoCMS 代码本身的一些难看的补丁解决了这个问题。

from cms import cms_menus
from cms.templatetags import cms_tags
from cms.utils import page


# Ugly-patching DjangoCMS so that a page from another Django Site can be displayed
def new_get_page_from_path(site, path, preview=False, draft=False):
    """
    Resolves a url path to a single page object.
    Returns None if page does not exist
    """
    from cms.models import Title

    titles = Title.objects.select_related('page__node')
    published_only = not draft and not preview

    if draft:
        titles = titles.filter(publisher_is_draft=True)
    elif preview:
        titles = titles.filter(publisher_is_draft=False)
    else:
        titles = titles.filter(published=True, publisher_is_draft=False)
    titles = titles.filter(path=(path or ''))
    titles = list(titles.iterator())

    for title in titles:
        if title.page.node.site_id != site.pk:
            continue

        if published_only and not page._page_is_published(title.page):
            continue

        title.page.title_cache = {title.language: title}
        return title.page

    # This is the different part from the DjangoCMS code:
    #    re do the same loop, but this time ignore the Site filtering
    for title in titles:
        if published_only and not page._page_is_published(title.page):
            continue

        title.page.title_cache = {title.language: title}
        return title.page
    return


# Ugly-patching DjangoCMS so that a page from another Django Site can fetched
# using {% pageurl %} (for example)
def new_get_page_queryset(site, draft=True, published=False):
    from cms.models import Page

    if draft:
        pages = Page.objects.drafts().on_site(site)
        if pages:
            return pages

    if published:
        pages = Page.objects.public().published(site)
        if pages:
            return pages

    pages = Page.objects.public().on_site(site)
    if pages:
        return pages

    # This is the different part from the DjangoCMS code:
    #    re do the same logic, but this time ignore the Site filtering

    if draft:
        return Page.objects.drafts()

    if published:
        return Page.objects.public().published()
    return Page.objects.public()


page.get_page_from_path = new_get_page_from_path
page.get_page_queryset = new_get_page_queryset
cms_tags.get_page_queryset = new_get_page_queryset
cms_menus.get_page_queryset = new_get_page_queryset

然后我在 urls.py 文件中的 urlpatterns 变量之前导入这个文件(警告你它很丑陋)。

DjangoCMS 所做的是尝试使用请求中给定的 Site 查找 Page。如果未找到 Page,DjangoCMS 将引发 404 错误,但在我们的例子中,我们重新执行相同的查询,但这次没有 Site 过滤器。

这样,在一个站点上创建的页面可以在每个子站点上访问。

然后我需要一些 Page 可以在每个 Site 上访问,其中大部分内容相同,但有些内容不同。我已经使用 static_placeholder 标签解决了这个问题,该标签可以为每个子 Site 指定。 http://docs.django-cms.org/en/latest/reference/templatetags.html#static-placeholder

关于python - DjangoCMS - 具有共享页面的多个站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61509218/

相关文章:

python - Django Admin 不保存保留在初始状态的预填充内联字段

python - 我如何使用 python 更改 pdf 中的超链接?

Django-cms自定义插件中的PlaceholderField,用于Placeholder,不可前端编辑

python - 如何使用 S3 和 Slack 集成编写 AWS lambda 函数

python - Django:同一浏览器中的两个 session

python - django-debug-toolbar 在获取 sql 统计信息时在管理员上中断

python - 如何捕获所有 View 中的所有错误?

python - django-cms apphook url 不适用于使用 Python shell 的 reverse()

Django CMS - 新页面上的外部重定向

python - 检查前 n 行的值是否都大于当前行的值