9

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?

thomallen
  • 1,926
  • 1
  • 18
  • 32

4 Answers4

13

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.

thomallen
  • 1,926
  • 1
  • 18
  • 32
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

MechanTOurS
  • 1,485
  • 2
  • 15
  • 18
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

Phares
  • 1,008
  • 13
  • 20
1

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

miga
  • 126
  • 5