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

时间:2022-09-24 23:29:11

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.

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

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?

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

4 个解决方案

#1


10  

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

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

app/template_tags/navigation_tags.py:

应用程序/ 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:

这要求您创建一个main-navigation.html文件。在这里,该模板中的HTML将被注入模板的任何位置,并且main-navigation.html可以访问自定义标记函数中传递给它的任何内容:

templates/main-navigation.html:

模板/主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.

我希望这可以帮助别人更好地理解模板标签。我发现文档在这个主题上有点混乱。

#2


2  

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

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

#3


1  

You can use Page model to get all published pages.

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

Page.objects.published()

You can use this in your views or plugins

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

Regards Miro migaat.blogspot.com

关心Miro migaat.blogspot.com

#4


1  

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

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

#1


10  

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

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

app/template_tags/navigation_tags.py:

应用程序/ 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:

这要求您创建一个main-navigation.html文件。在这里,该模板中的HTML将被注入模板的任何位置,并且main-navigation.html可以访问自定义标记函数中传递给它的任何内容:

templates/main-navigation.html:

模板/主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.

我希望这可以帮助别人更好地理解模板标签。我发现文档在这个主题上有点混乱。

#2


2  

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

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

#3


1  

You can use Page model to get all published pages.

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

Page.objects.published()

You can use this in your views or plugins

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

Regards Miro migaat.blogspot.com

关心Miro migaat.blogspot.com

#4


1  

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

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