如何从每个页面访问 django-cms 中的所有页面对象?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最终通过在 django 中创建一个提供所有 cms 页面的 templatetag 来解决这个问题:
app/template_tags/navigation_tags.py:
然后在模板中,您可以按如下方式调用模板标记:
这要求您有一个 main-navigation .html 文件已创建。然后,无论标签在哪里,该模板中的 HTML 都将被注入到模板中,并且 main-navigation.html 将可以访问自定义标签函数中传递给它的任何内容:
templates/main-navigation.html:
我希望这样帮助人们更好地理解模板标签。我发现文档在这个主题上有点令人困惑。
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:
Then in the templates you call the template tag as follows:
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:
I hope this helps someone better understand templatetags. I found the documentation a bit confusing on this subject.
根据您应该使用的文档:
source: https://github.com/divio/django-cms/blob/support/2.4.x/cms/models/managers.py#L31
According to the doc you should use:
source: https://github.com/divio/django-cms/blob/support/2.4.x/cms/models/managers.py#L31
您可以使用页面模型来获取所有已发布的页面。
您可以在您的视图或插件中使用它
问候
米罗
migaat.blogspot.com
You can use Page model to get all published pages.
You can use this in your views or plugins
Regards
Miro
migaat.blogspot.com
您需要将其添加到您想要的页面位置。
这对我有用。
您可能需要在代码中的某个位置包含
{% load staticfiles %}
You need to add this where you want the page.
This worked for me.
You may need to have included
{% load staticfiles %}
somewhere in your code