[Django]-DjangoCMS, use snippets outside of CMS templates

2đź‘Ť

âś…

There are two ways I know of to do this in Django-CMS…

One is to create a Django-CMS page and hook your django-contact-form’s urls to it.

The other is to create a shared cms page that isn’t included the navigation with a placeholder that you can reference by ID in your django-contact-form app’s template(s).

Hope that gets you going.

3đź‘Ť

I found a good solution in this django-cms issue. Create a template tag, ala something/templatetags/show_snippet_tag.py:

# From https://github.com/divio/django-cms/issues/491

from cms.plugins.snippet.models import Snippet
from cms.plugins.snippet.cms_plugins import SnippetPlugin
from django import template

register = template.Library()

@register.simple_tag
def show_snippet(name):
    try:
        return SnippetPlugin().render(
            {},
            type('obj', (object,), {'snippet' : Snippet.objects.get(name = name)}),
            None
        )['content']
    except Snippet.DoesNotExist:
        return "[No snippet named '%s']" % name

And then in your template,

{% load show_snippet_tag %}
{% show_snippet "My Snippet Name" %}

Kaboom.

👤a paid nerd

0đź‘Ť

Another thought would be that instead of a snippet, you might be able to more easily use a “static_placeholder” – if you are using the new DjangoCMS 3.x. This then has the added benefit of making it easy for content editors to access and edit the content on every page it is in rather than having to go to the admin interface and hunt down the appropriate snippet name separately. It will also allow the editor-person to use the more friendly WYSIWYG interface (rather than the manual-code of the snippet) or even add a forms plugin that you have added to your project space.

You can see the quick docs on them here: http://django-cms.readthedocs.org/en/latest/advanced/templatetags.html#static-placeholder

👤The NetYeti

Leave a comment