DjangoCMS, use snippets outside of CMS templates
I'm trying to use django-contact-form in django-cms. django-contact-form has one or two pages (showing a form and a thank-you note), which are represented by django templates
In the left column of these templates I would love to show some general information on contacting us, which should be editable from the CMS.
Now I suppose I could use a {% placeholder "contact_info" %}
somewhere. But there isn't really the concept of a "contact page", since that's already taken care of by the contact_form app, which simply provides templates (no cms stuff)
My understanding is that I'd need to create some kind of fake contact page & an associated templ开发者_运维知识库ate to even get this to work.
What I'm trying to say is: I want to use an editable snipplet of text (or whatever, but in this case text), from template. How do I do this in Django-CMS?
Incidentally, the link below seems to indicate that this is possible, but not how it's possible.
https://github.com/divio/django-cms/issues/491
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.
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.
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
精彩评论