Wagtail CMS注入应用程序可插入HTML代码

时间:2018-10-03 22:14:11

标签: wagtail

希望您可以通过我一直在努力的Wagtail CMS(Django)应用程序来帮助我。

我创建了一个名为wagtailinjector的新应用。该应用程序的目的是在关闭标签(在打开的身体标签()之后和在关闭的身体标签(

但是我不确定我在做什么错,因为它不起作用。你能帮我吗?

我进行了以下设置:

models.py

from django.db import models

from wagtail.contrib.settings.models import BaseSetting, register_setting
from wagtail.admin.edit_handlers import FieldPanel


class InjectorMixin(object):
    """
    An object that can be injected into the HTML document.
    """

    def get_code_before_head_close(self):
        raise NotImplementedError()

    def get_code_after_body_open(self):
        raise NotImplementedError()

    def get_code_before_body_close(self):
        raise NotImplementedError()

@register_setting(icon='tag')
class InjectorSettingsMixin(BaseSetting):
    before_head_close = models.TextField(
        help_text='Inject HTML before the closing head tag (</head>) of a document',
        blank=True
    )
    after_body_open = models.TextField(
        help_text='Inject HTML after the opening body tag (<body>) of a document',
        blank=True
    )
    before_body_close = models.TextField(
        help_text='Inject HTML before the closing body tag (</body>) of a document',
        blank=True
    )

    panels = [
        FieldPanel('before_head_close'),
        FieldPanel('after_body_open'),
        FieldPanel('before_body_close'),
    ]

    def get_code_before_head_close(self):
        return self.before_head_close

    def get_code_after_body_open(self):
        return self.after_body_open

    def get_code_before_body_close(self):
        return self.before_body_close

    class Meta:
      verbose_name = 'Injector'

tags.py

from django.template.loader import render_to_string

def before_head_close(request, model):
    context = {
        'site_name': request.site.site_name,
        'object': model,
    }

    return render_to_string('wagtailinjector/parts/tags.html',
                            context, request=request)

templatetags / wagtailinjector.py

from django import template

from wagtailinjector import tags

register = template.Library()

@register.simple_tag()
def before_head_close():
    request = context['request']
    if not model:
        model = context['self']

    return tags.before_head_close(request, model)

templates / wagtailinjector / parts / tags.html

{% block tags %}
{{ object.get_code_before_head_close }}
{% endblock tags %}

0 个答案:

没有答案