Frontend editing for Page and Django models

    As well as , ‘ordinary’ Django model fields (both on CMS Pages and your own Django models) can also be edited through django CMS’s frontend editing interface. This is very convenient for the user because it saves having to switch between frontend and admin views.

    Using this interface, model instance values that can be edited show the “Double-click to edit” hint on hover. Double-clicking opens a pop-up window containing the change form for that model.

    Note

    This interface is not currently available for touch-screen users, but will be improved in future releases.

    Warning

    By default and for consistency with previous releases, templatetags used by this feature mark as safe the content of the rendered model attribute. This may be a security risk if used on fields which may hold non-trusted content. Be aware, and use the templatetags accordingly. To change this behaviour, set the setting: CMS_UNESCAPED_RENDER_MODEL_TAGS to False.

    Warning

    This feature is only partially compatible with django-hvad: using render_model with hvad-translated fields (say {% render_model object 'translated_field' %} returns an error if the hvad-enabled object does not exists in the current language. As a workaround render_model_icon can be used instead.

    This feature relies on five template tags sharing common code. All require that you {% load cms_tags %} in your template:

    • render_model (for editing a specific field)
    • (for editing any of the fields in a defined block)
    • render_model_icon (for editing a field represented by another value, such as an image)
    • (for adding an instance of the specified model)
    • render_model_add_block (for adding an instance of the specified model)

    Look at the tag-specific page for more detailed reference and discussion of limitations and caveats.

    Page titles edit

    For CMS pages you can edit the titles from the frontend; according to the attribute specified a default field, which can also be overridden, will be editable.

    Main title:

    1. {% render_model request.current_page "page_title" %}

    Menu title:

    1. {% render_model request.current_page "menu_title" %}

    All three titles:

    1. {% render_model request.current_page "titles" %}

    You can always customise the editable fields by providing the edit_field parameter:

    1. {% render_model request.current_page "title" "page_title,menu_title" %}

    By using the special keyword changelist as edit field the frontend editing will show the page tree; a common pattern for this is to enable changes in the menu by wrapping the menu template tags:

    Will render to:

    1. <div class="cms-plugin cms-plugin-cms-page-changelist-1">
    2. <h3>Menu</h3>
    3. <li><a href="/">Home</a></li>
    4. <li><a href="/another">another</a></li>
    5. [...]
    6. </div>

    Editing ‘ordinary’ Django models

    As noted above, your own Django models can also present their fields for editing in the frontend. This is achieved by using the FrontendEditableAdminMixin base class.

    Note that this is only required for fields other than PlaceholderFields. PlaceholderFields are automatically made available for frontend editing.

    Configure your admin class by adding the FrontendEditableAdminMixin mixin to it (see for general Django admin information):

    1. from cms.admin.placeholderadmin import FrontendEditableAdminMixin
    2. class MyModelAdmin(FrontendEditableAdminMixin, admin.ModelAdmin):
    3. ...

    The ordering is important: as usual, mixins must come first.

    Then set up the templates where you want to expose the model for editing, adding a render_model template tag:

    1. {% load cms_tags %}
    2. {% block content %}
    3. <h1>{% render_model instance "some_attribute" %}</h1>
    4. {% endblock content %}

    See templatetag reference for arguments documentation.

    Frontend editing is also possible for a set of fields.

    Set up the admin

    1. from cms.admin.placeholderadmin import FrontendEditableAdminMixin
    2. from django.contrib import admin
    3. class MyModelAdmin(FrontendEditableAdminMixin, admin.ModelAdmin):
    4. frontend_editable_fields = ("foo", "bar")
    5. ...

    Set up the template

    Then add comma separated list of fields (or just the name of one field) to the template tag:

    The attribute argument of the template tag is not required to be a model field, property or method can also be used as target; in case of a method, it will be called with request as argument.

    You can link any field to a custom view (not necessarily an admin view) to handle model-specific editing workflow.

    The custom view can be passed either as a named url (view_url parameter) or as name of a method (or property) on the instance being edited (view_method parameter). In case you provide view_method it will be called whenever the template tag is evaluated with request as parameter.

    The custom view does not need to obey any specific interface; it will get edit_fields value as a GET parameter.

    See for arguments documentation.

    Example view_url:

    1. {% load cms_tags %}
    2. <h1>{% render_model instance "some_attribute" "some_field,other_field" "" "admin:exampleapp_example1_some_view" %}</h1>
    3. {% endblock content %}

    Example view_method:

    1. class MyModel(models.Model):
    2. char = models.CharField(max_length=10)
    3. def some_method(self, request):
    4. return "/some/url"
    5. {% load cms_tags %}
    6. {% block content %}
    7. <h1>{% render_model instance "some_attribute" "some_field,other_field" "" "" "some_method" %}</h1>
    8. {% endblock content %}

    By using the special keyword changelist as edit field the frontend editing will show the model changelist:

    1. {% render_model instance "name" "changelist" %}

    Will render to:

    1. <div class="cms-plugin cms-plugin-myapp-mymodel-changelist-1">
    2. My Model Instance Name
    3. </div>

    If you need to apply filters to the output value of the template tag, add quoted sequence of filters as in Django filter template tag:

    Context variable

    The template tag output can be saved in a context variable for later use, using the standard as syntax:

    1. {% load cms_tags %}
    2. {% block content %}
    3. {% render_model instance "attribute" as variable %}
    4. <h1>{{ variable }}</h1>
    5. {% endblock content %}