Custom Page Types
=================
In many cases, the built-in page types will be exactly what you need. There are, however,
several reasons why you may need a custom page type. This tutorial will show you an example
for creating a custom page type.
Let's say that we need to make a special product page for all of our cupcakes. While our real bakery
may have over 100 different types, we will limit this example to a small handful but enough to show
how this works.
Before we begin, you should have a general understanding of `Django models `_
and some Python skills. You can still follow along for an introduction to these concepts even without this knowledge.
We are also going to be unable to cover every potential use case or scenario in this tutorial, but we hope that it will springboard
any ideas that you have for your own website.
Prep work for custom pages
--------------------------
We need to plan our page ahead of time. What fields will our custom page need, and what will we need our page
to do? Take the time to write down the answer to these questions before you even touch the code. This is what
we are writing down for Simple Sweet Dessert's custom cupcake page:
**Our Prep Notes**
1. We want our page to page to list the attributes and descriptions of individual cupcakes.
2. We want to be able to display the cupcakes in cards automatically on a landing page.
**Cupcake Page Fields:**
* Name of cupcake (This could be the title of the page)
* Photo of cupcake
* Description of cupcake
* Days when these cupcakes are made
**Cupcake Landing Page Fields:**
* Needs to be the parent page for the cupcake pages
Setting up the page models
--------------------------
Just like in Django or Wagtail, you will need to set up your page models in the ``models.py`` file of your
project. Navigate to ``mysite\website\models.py`` in your code editor and open up the ``models.py`` file.
You should already see a few page models in there from CodeRed CMS, as well as imports at the top from the
frameworks that we are using.
.. code-block:: python
"""
Creatable pages used in CodeRed CMS.
"""
from modelcluster.fields import ParentalKey
from coderedcms.forms import CoderedFormField
from coderedcms.models import (
CoderedArticlePage,
CoderedArticleIndexPage,
CoderedEmail,
CoderedFormPage,
CoderedWebPage
)
class ArticlePage(CoderedArticlePage):
"""
Article, suitable for news or blog content.
"""
class Meta:
verbose_name = 'Article'
ordering = ['-first_published_at']
# Only allow this page to be created beneath an ArticleIndexPage.
parent_page_types = ['website.ArticleIndexPage']
template = 'coderedcms/pages/article_page.html'
search_template = 'coderedcms/pages/article_page.search.html'
class ArticleIndexPage(CoderedArticleIndexPage):
"""
Shows a list of article sub-pages.
"""
class Meta:
verbose_name = 'Article Landing Page'
# Override to specify custom index ordering choice/default.
index_query_pagemodel = 'website.ArticlePage'
# Only allow ArticlePages beneath this page.
subpage_types = ['website.ArticlePage']
template = 'coderedcms/pages/article_index_page.html'
class FormPage(CoderedFormPage):
"""
A page with an html