Pages and Sections#
Overview#
Pages and sections are the basic building blocks of your website. Pages define regions in which sections can be placed. And sections can be any kind of content you want to see on your website.
Pages and sections are defined by subclassing the Page and
Section model:
from simple_page.models import Page, Section
class FancyPage(Page):
REGIONS = [
('main', 'Main Region'),
('sidebar', 'Sidebar'),
('footer', 'Footer'),
]
class Meta:
proxy = True
class FancySection(Section):
title = models.CharField(max_length=255, blank=True)
text = models.TextField(blank=True)
With those two models you are able to build a simple website.
Api Reference#
- class simple_page.models.Page(*args, **kwargs)#
Bases:
MPTTModelBase model for all pages.
Each subclass has to do is to setup its
regionsand define aget_absolute_url()method. Since the database layout is fully functional, you may define your own page model as a proxy if you have no need for additional database fields.Sections associated with a page are accessible by their region. Use the region’s name to get a queryset of sections belonging to that region.
The page model is tree structured by django-mptt.
- REGIONS = []#
REGIONS must be set by subclasses as a list of tuples holding the region’s name and its title. Something like:
REGIONS = [ ('main', 'Main Region'), ('sidebar', 'Sidebar'), ('footer', 'Footer'), ]
- REGION_SECTIONS = {}#
Limit the section models allowed within a specific region with a mapping like this:
REGION_SECTIONS = { 'main': [FancySection, AnotherFancySection], 'sidebar': [SidebarSection], }
If a region is not listed all section models are allowed within that region. To completely disallow sections within a region set the region’s value to None. The region then won’t be listed in the admin interface for that page model. This is useful if you want to provide sections dynamically for a specific region. See the
get_region_sections()method for more information.
- resolved_obj#
Return the instance of the child class.
- class simple_page.models.Section(*args, **kwargs)#
Bases:
ModelBase model for what ever content you want to see on your website. It does not has any fields by its own but can be equipped by sublcasses.
Sections are related to pages via a many-to-many relationship that holds the region in which a section should be rendered and an index field to make the sections orderable whithin that region.
- objects = <model_utils.managers.InheritanceManager object>#
We use the InheritanceManager to provide a simple api to access child class objects.
- class simple_page.models.PageSection(*args, **kwargs)#
Bases:
ModelPageSection is the intermediate model for the many-to-many relationship between pages and sections. It holds the region in which a section should be rendered and an index field to make sections orderable whithin that region.