Skip to content
ccnmtl edited this page May 4, 2011 · 3 revisions

django-pagetree and django-pageblocks are bits and pieces to create a lightweight CMS oriented around a hierarchy (or hierarchies) of pages with various types of content which is generally intended for moving through linearly.

The typical use-case is when a site is structured like

  • Section 1
    • Section 1.1
      • Section 1.1.1
      • Section 1.1.2
      • Section 1.1.3
    • Section 1.2
      • Section 1.2.1
      • Section 1.2.2
  • Section 2

And a typical user will start from Section 1, then proceed to Section 1.1, Section 1.1.1, etc. basically in a depth-first traversal. Each section can contain "content" like text, images, videos, etc. as well as interactive elements such as quizzes, etc. The types of content on these pages are defined in code as Pageblocks and editable by an admin through a web interface.

To make a pagetree site, you will need to install django-pagetree, django-pageblocks, and treebeard (probably a few other modules that they depend on) and add them to INSTALLED_APPS. In your settings, you'll also need to define PAGEBLOCKS like so:

PAGEBLOCKS = ['pageblocks.TextBlock',
              'pageblocks.HTMLBlock',
              'pageblocks.PullQuoteBlock',
              'pageblocks.ImageBlock',
              'pageblocks.ImagePullQuoteBlock']

This tells the user interface which types of pageblocks to make available. If you create your own custom pageblock types, you'll add them there. This just lists the default ones that django-pageblocks provides.

Pagetree only provides various helpers, so you'll still need to create a main application for your site where you can define views. Eg,

$ ./manage.py startapp main

In your urls.py you will need to set a few things up. First, pagetree provides some views of its own, so you'll need:

 (r'^pagetree/',include('pagetree.urls')),

In there. Next, to allow a view in your application to serve up pagetree pages, you'll want to the following rules at the end:

(r'^edit/(?P<path>.*)$','mysite.main.views.edit_page'),
(r'^(?P<path>.*)$','mysite.main.views.page'),

It's important that those be at the end of the list since they'll try to basically catch any URLs that haven't matched elsewhere.

As you can see, those point at views that need to be defined in your application. So in main/views.py, you need page and edit_page views. The very simple ones to start with (I'm going to assume some of the standard django import boilerplate to save space):

from pagetree.helpers import get_hierarchy, get_section_from_path, get_module, needs_submit, submitted

@rendered_with('main/page.html')
def page(request,path):
    section = get_section_from_path(path)
    root = section.hierarchy.get_root()
    module = get_module(section)
    return dict(section=section,
                module=module,
                modules=root.get_children(),
                root=section.hierarchy.get_root()
                )

@rendered_with('main/edit_page.html')
def edit_page(request,path):
    section = get_section_from_path(path)
    return dict(section=section,
                module=get_module(section),
                root=section.hierarchy.get_root())
Clone this wiki locally