Creating page types programatically

87 views
Skip to first unread message

Patrick Heneghan

unread,
Sep 6, 2016, 7:41:44 AM9/6/16
to django CMS developers

Posted already to the Github issue tracker, but it was suggested I post here instead:


In other CMS's I've used, it's been possible to define Page Types (or "post types") programatically. This is desirable, as it allows me to commit these to code, and have them ready for use when the site has been installed - rather than going through the manual process of creating them via the admin area post-installation.


Is this possible in Django CMS? It doesn't seem to be documented.

Iacopo Spalletti

unread,
Sep 6, 2016, 7:58:32 AM9/6/16
to django-cms...@googlegroups.com
Do you mean "Page Type" in the django CMS meaning (ie: special pages
prefilled with content) or just page templates? In the latter django
templates configured in CMS_TEMPLATES are what you are looking for.

In the former case django CMS Page types are just pages saved under a
special parent.
If you want to ship them together with the code you have two options:
- saving a fixture file with the pre-saved pages
- create a datamigration which creates the pages using create_page /
create_title / add_plugin public API
(http://django-cms.readthedocs.io/en/release-3.3.x/reference/api.html)

Hope this helps

>
> --
> Message URL: *MailScanner has detected definite fraud in the website at
> "groups.google.com". Do /not/ trust this website:*
> https://groups.google.com/d/msg/django-cms-developers/topic-id/message-id <https://groups.google.com/d/msg/django-cms-developers/topic-id/message-id>
> Unsubscribe: send a message to
> django-cms-devel...@googlegroups.com
> ---
> You received this message because you are subscribed to the Google
> Groups "django CMS developers" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-cms-devel...@googlegroups.com
> <mailto:django-cms-devel...@googlegroups.com>.
> To view this discussion on the web, visit *MailScanner has detected
> definite fraud in the website at "groups.google.com". Do /not/ trust
> this website:*
> https://groups.google.com/d/msgid/django-cms-developers/04effbc0-fe73-4fc8-a17a-325ab82d701f%40googlegroups.com
> <https://groups.google.com/d/msgid/django-cms-developers/04effbc0-fe73-4fc8-a17a-325ab82d701f%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit *MailScanner has detected definite fraud in the
> website at "groups.google.com". Do /not/ trust this website:*
> https://groups.google.com/d/optout <https://groups.google.com/d/optout>.


--
Iacopo Spalletti

Nephila s.a.s. - Firenze
Telefono: +39 055 5357189
Assistenza Tecnica: +39 055 3985730
http://nephi.la

signature.asc

Patrick Heneghan

unread,
Sep 6, 2016, 8:10:13 AM9/6/16
to django CMS developers
Hi Iacopo,

It might help for me to explain this in context - for example, I'm going to have a "post type" called "blog", which should have title and content fields, and then "event", which should have additional location, date, and time fields.

I know it would be possible for the user to create a page with the required fields by dragging and dropping the correct plugins onto the page. However, I would like the user to be able to simply click "new Event" and have those fields already on the page, waiting to be filled in.

So, I think what I need is a Page Type - which would already contain the correct empty fields, but not yet contain any content. Would you agree?

Philipp Zedler

unread,
Sep 6, 2016, 8:35:06 AM9/6/16
to django-cms...@googlegroups.com
Hi Patrick,

here's some example code.

Context: Sometimes I put links into the toolbar menu where the editors
can add new things, like tv broadcasts, or clay vases. When the new
thing is a CMSPlugin and should appear on a newly created page, I let
their model class inherit from this mixin and call `ensure_is_in_page`
from the admin class.

class CreatePageMixin(object):

def ensure_is_in_page(self, user, parent_page_id):
try:
self.placeholder.page.pk
except AttributeError:
page = self.create_page(user, parent_page_id)
self.assign_to_page(page)


def create_page(self, user, parent_page_id):
from cms import api # Not available before model creation
try:
parent = Page.objects.get(pk=parent_page_id)
except Page.DoesNotExist:
parent = self.get_parent_page() # to implement if required
page = api.create_page(
self.name,
self.PAGE_TEMPLATE, # define this in each child class
'de', # or any other language
created_by=user,
parent=parent
)
return page

def assign_to_page(self, page):
placeholder = page.placeholders.get(slot=self.PLACEHOLDER)
self.placeholder = placeholder
self.language = 'de'
self.position = 0
self.plugin_type = self.PLUGIN_TYPE

That's probably not exactly what you need but should give you an idea of
what's possible.

Best
Philipp



--
Philipp Zedler
Developer für's Web
Am langen Rain 12
39031 Bruneck
Italien
++39 / 324 / 77 345 96
http://www.zedler.it

Iacopo Spalletti

unread,
Sep 6, 2016, 8:47:30 AM9/6/16
to django-cms...@googlegroups.com
Bear in mind that django CMS Pages are not somethign you can extend,
except than using Page/Title extensions
(http://django-cms.readthedocs.io/en/release-3.3.x/how_to/extending_page_title.html),
which are not really intended to add the content of the page but rather,
ancillary data or metadata.

You may want to explore the possibility to use dedicated Django
applications for structured content (say aldryn-newsblog or
djangocms-blog to handle blog posts etc)
signature.asc

Daniele Procida

unread,
Sep 9, 2016, 8:43:38 AM9/9/16
to django CMS developers
On Tue, Sep 6, 2016, Patrick Heneghan <patrickh...@gmail.com> wrote:

>It might help for me to explain this in context - for example, I'm going to
>have a "post type" called "blog", which should have title and content
>fields, and then "event", which should have additional location, date, and
>time fields.

Hi Patrick.

I hadn't realised that by Page Type you meant, in effect, a different content model.

Page Types in django CMS are a way to take a snapshot of a page for resuse, to save you having to re-do a complex layout that you often reuse.

If you want to manage weblog content, or events, use a weblog or events application, so that instead of shoe-horning information into unsuitable containers, you can have models that are designed to hold it in the most useful way.

Other CMSes (like Wagtail and Mezzanine) extend their pages in the way you describe, but django CMS has always avoided that as a core principle.

Daniele

Reply all
Reply to author
Forward
0 new messages