5. Placeholders outside the CMS
Placeholders are special model fields that django CMS uses to render user-editable content (plugins) in templates. That is, it’s the place where a user can add text, video or any other plugin to a webpage, using either the normal Django admin interface or the so called frontend editing.
Placeholders can be viewed as containers for CMSPlugin
instances, and can be used outside the CMS in custom applications using the PlaceholderField
.
By defining one (or several) PlaceholderField
on a custom model you can take advantage of the full power of CMSPlugin
, including frontend editing.
5.1. Quickstart
You need to define a PlaceholderField
on the model you would like to use:
from django.db import models
from cms.models.fields import PlaceholderField
class MyModel(models.Model):
# your fields
my_placeholder = PlaceholderField('placeholder_name')
# your methods
The PlaceholderField
takes a string as first argument which will be used to configure which plugins can be used in this placeholder. The configuration is the same as for placeholders in the CMS.
If you install this model in the admin application, you have to use PlaceholderAdmin
instead of ModelAdmin
so the interface renders correctly:
from django.contrib import admin
from cms.admin.placeholderadmin import PlaceholderAdmin
from myapp import MyModel
admin.site.register(MyModel, PlaceholderAdmin)
Now to render the placeholder in a template you use the render_placeholder
tag from the placeholder_tags
template tag library:
{% load placeholder_tags %}
{% render_placeholder mymodel_instance.my_placeholder "640" %}
The render_placeholder
tag takes a PlaceholderField
instance as first argument and optionally accepts a width parameter as second argument for context sensitive plugins.
5.2. Adding content to a placeholder
There are two ways to add or edit content to a placeholder, the front-end admin view and the back-end view.
5.2.1. Using the front-end editor
Probably the most simple way to add content to a placeholder, simply visit the page displaying your model (where you put the render_placeholder
tag), then append ?edit
to the page’s URL. This will make a top banner appear, and after switching the “Edit mode” button to “on”, the banner will prompt you for your username and password (the user should be allowed to edit the page, obviously).
You are now using the so-called front-end edit mode:
Once in Front-end editing mode, your placeholders should display a menu, allowing you to add plugins to them: the following screen shot shows a default selection of plugins in an empty placeholder.
Plugins are rendered at once, so you can have an idea what it will look like in fine, but to view the final look of a plugin simply leave edit mode by clicking the “Edit mode” button in the banner again.
5.3. Fieldsets
There are some hard restrictions if you want to add custom fieldsets to an admin page with at least one PlaceholderField
:
- Every
PlaceholderField
must be in it’s ownfieldset
, onePlaceholderField
per fieldset. - You must include the following two classes:
'plugin-holder'
and'plugin-holder-nopage'