如何从每个页面访问 django-cms 中的所有页面对象?

发布于 2024-10-06 12:15:38 字数 315 浏览 0 评论 0原文

我正在使用 Django CMS 2.1.0.beta3 并遇到问题。我需要访问变量中的所有页面,以便可以循环它们并使用 for 循环创建导航菜单。 django cms 提供的 show_menu 功能不适用于我正在做的事情。

我需要一个包含所有页面的查询集,以便我可以执行类似于以下操作的操作:

{% for page in cms_pages %}
    {{ page.title }}
{% endfor %}    

有谁知道如何访问所有已发布的页面对象,例如 ALL 页面上的对象?

I am using Django CMS 2.1.0.beta3 and am encountering a problem. I need to have access to all the pages in a variable so that I can loop through them and create my navigation menu using a for loop. The show_menu functionality provided with django cms will not work for what I am doing.

I need a queryset with all pages so I can do something similar to the following:

{% for page in cms_pages %}
    {{ page.title }}
{% endfor %}    

Does anyone know how I can gain access to all the published page objects like that on ALL pages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰之心 2024-10-13 12:15:38

我最终通过在 django 中创建一个提供所有 cms 页面的 templatetag 来解决这个问题:

app/template_tags/navigation_tags.py:

from django import template
from cms.models.pagemodel import Page

register = template.Library()

def cms_navigation():
    cms_pages = Page.objects.filter(in_navigation=True, published=True)
    return {'cms_pages': cms_pages}

register.inclusion_tag('main-navigation.html')(cms_navigation)

然后在模板中,您可以按如下方式调用模板标记:

{% load navigation_tags %} {% cms_navigation %}

这要求您有一个 main-navigation .html 文件已创建。然后,无论标签在哪里,该模板中的 HTML 都将被注入到模板中,并且 main-navigation.html 将可以访问自定义标签函数中传递给它的任何内容:

templates/main-navigation.html:

<ul id="navigation">
    {% for page in cms_pages %}
         {{ page.get_title }}
    {% endfor %}    
</ul>

我希望这样帮助人们更好地理解模板标签。我发现文档在这个主题上有点令人困惑。

I ended up solving this problem by creating a templatetag in django that serves up all of the cms pages:

app/template_tags/navigation_tags.py:

from django import template
from cms.models.pagemodel import Page

register = template.Library()

def cms_navigation():
    cms_pages = Page.objects.filter(in_navigation=True, published=True)
    return {'cms_pages': cms_pages}

register.inclusion_tag('main-navigation.html')(cms_navigation)

Then in the templates you call the template tag as follows:

{% load navigation_tags %} {% cms_navigation %}

This requires that you have a main-navigation.html file created. Here then the HTML from that template will be injected into the template wherever the tag is and the main-navigation.html will have access to whatever was passed to it in the custom tag function:

templates/main-navigation.html:

<ul id="navigation">
    {% for page in cms_pages %}
         {{ page.get_title }}
    {% endfor %}    
</ul>

I hope this helps someone better understand templatetags. I found the documentation a bit confusing on this subject.

So要识趣 2024-10-13 12:15:38

根据您应该使用的文档:

Page.objects.public()

source: https://github.com/divio/django-cms/blob/support/2.4.x/cms/models/managers.py#L31

According to the doc you should use:

Page.objects.public()

source: https://github.com/divio/django-cms/blob/support/2.4.x/cms/models/managers.py#L31

丢了幸福的猪 2024-10-13 12:15:38

您可以使用页面模型来获取所有已发布的页面。

Page.objects.published()

您可以在您的视图或插件中使用它

问候
米罗
migaat.blogspot.com

You can use Page model to get all published pages.

Page.objects.published()

You can use this in your views or plugins

Regards
Miro
migaat.blogspot.com

枫以 2024-10-13 12:15:38

您需要将其添加到您想要的页面位置。

{{ request.current_page }}

这对我有用。
您可能需要在代码中的某个位置包含 {% load staticfiles %}

You need to add this where you want the page.

{{ request.current_page }}

This worked for me.
You may need to have included {% load staticfiles %} somewhere in your code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文