DjangoCMS,在 CMS 模板之外使用片段

发布于 2024-11-17 12:45:26 字数 577 浏览 5 评论 0原文

我正在尝试在 django-cms 中使用 django-contact-form。 django-contact-form 有一两页(显示表单和感谢信),由 django 模板表示

在这些模板的左栏中,我希望显示一些有关联系我们的一般信息,这些信息应该是可从 CMS 进行编辑。

现在我想我可以在某处使用 {% placeholder "contact_info" %} 。但实际上并没有“联系页面”的概念,因为这已经由 contact_form 应用程序处理了,它只是提供模板(没有 cms 的东西)

我的理解是我需要创建某种虚假联系人页和一个相关的模板甚至可以让它工作。

我想说的是:我想使用模板中的可编辑文本片段(或其他内容,但在本例中为文本)。我如何在 Django-CMS 中执行此操作?

顺便说一句,下面的链接似乎表明这是可能的,但没有表明这是如何可能的。

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

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 template 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷荒 2024-11-24 12:45:26

我在这个 django-cms 问题中找到了一个很好的解决方案。创建一个模板标签,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

然后在您的模板中,

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

Kaboom。

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.

好听的两个字的网名 2024-11-24 12:45:26

我知道在 Django-CMS 中执行此操作有两种方法...

一种是创建 Django-CMS 页面和 将 django-contact-form 的 url 挂钩到它

另一种是创建一个共享的 cms 页面,该页面不包含带有占位符的导航,您可以在 django-contact-form 应用程序模板中通过 ID 引用该占位符。

希望这能让你继续前进。

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.

冷清清 2024-11-24 12:45:26

另一个想法是,如果您使用新的 DjangoCMS 3.x,您可能可以更轻松地使用“static_placeholder”而不是片段。这样还有一个额外的好处,即内容编辑者可以轻松访问和编辑其所在每个页面上的内容,而不必转到管理界面并单独查找适当的代码片段名称。它还将允许编辑人员使用更友好的所见即所得界面(而不是代码片段的手动代码),甚至添加您已添加到项目空间中的表单插件。

您可以在此处查看有关它们的快速文档: http:// /django-cms.readthedocs.org/en/latest/advanced/templatetags.html#static-placeholder

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文