Closed macolo closed 5 months ago
djangocms-rest enables frontend projects to consume django CMS content through a browseable read-only, REST/JSON API. It is based on the django rest framework (DRF).
For the following topics please see the ../README.md
A Headless CMS (Content Management System) is a backend-only content management system that provides content through APIs, making it decoupled from the front-end presentation layer. This allows developers to deliver content to any device or platform, such as websites, mobile apps, or IoT devices, using any technology stack. By separating content management from content presentation, a Headless CMS offers greater flexibility and scalability in delivering content.
Running a CMS in headless mode offers several benefits, including greater flexibility in delivering content to multiple platforms and devices through APIs, enabling consistent and efficient multi-channel experiences. It enhances performance and scalability by allowing frontend and backend development to progress independently using the best-suited technologies. Additionally, it streamlines content management, making it easier to update and maintain content across various applications without needing to alter the underlying infrastructure.
The good news first: django CMS headless mode is fully backend supported and works independently from the javascript framework. It is fully compatible with the javascript framework of your choosing.
It's pretty much the same as for a traditional django CMS project, see here for instructions on how to create django CMS plugins.
Let's have an example. Here is a simple plugin with two fields to render a custom header. Please note that the template included is just a simple visual helper to support editors to manage content in the django CMS backend. Also, backend developers can now toy around and test their django CMS code independently from a frontend project.
After setting up djangocms-rest and creating such a plugin you can now run the project and see a REST/JSON representation of your content in your browser, ready for consumption by a decoupled frontend.
cms_plugins.py
:
# -*- coding: utf-8 -*-
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from . import models
class CustomHeadingPlugin(CMSPluginBase):
model = models.CustomHeadingPluginModel
module = 'Layout Helpers'
name = "My Custom Heading"
# this is just a simple, unstyled helper rendering so editors can manage content
render_template = 'custom_heading_plugin/plugins/custom-heading.html'
allow_children = False
plugin_pool.register_plugin(CustomHeadingPlugin)
models.py
:
from cms.models.pluginmodel import CMSPlugin
from django.db import models
class CustomHeadingPluginModel(CMSPlugin):
heading_text = models.CharField(
max_length=256,
)
size = models.PositiveIntegerField(default=1)
templates/custom_heading_plugin/plugins/custom-heading.html
:
<h{{ instance.size }} class="custom-header">{{ instance.heading_text }}</h{{ instance.size }}>
Yes, djangocms-rest provides out of the box support for any and all django CMS plugins whose content can be serialized.
Yes, djangocms-text has both HTML blob and structured JSON support for rich text.
URLs to other CMS objects are dynamic, in the form of cms.object-name:<uid>
, for example cms.page:2
. The frontend can then use this to resolve the object and create the appropriate URLs to the object's frontend representation.
Absolutely, you can use the djangocms-aliases package. It allows you to define custom placeholders that are not linked to any pages. djangocms-rest will then make a list of those aliases and their content available via the REST API.
@macolo Thanks! Sounds great! Moved it to the readme :-)
Developers will want an introduction in how to approach headless projects with django CMS. Maybe a FAQ will help?