Wagtail:如何覆盖数据库的HTML标记输出。使用<strong>或<em>代替<b>或<i> for richtext模板标记

时间:2018-04-18 14:54:46

标签: django django-templates wagtail hallo-js

我正在尝试使用我的wagtail模板输出<strong></strong>而不是<b></b><em></em>而不是<i></i>

我手动编辑了wagtailcore_pagerevision表记录中的content_json值,以便<b>标记为<strong><i>标记为<em>,但输出HTML继续输出<{1}}和<b>分别为。{/ p>

在我的模板中,我有<i>用于阻止,{{ block.value|richtext }}用于非阻止。

做这项工作的不良代码是:

{{ self.body|richtext }}

我的问题是..我怎么能告诉Wagtail或Django使用@register.filter def richtext(value): if isinstance(value, RichText): # passing a RichText value through the |richtext filter should have no effect return value elif value is None: html = '' else: html = expand_db_html(value) return mark_safe('<div class="rich-text">' + html + '</div>') <strong>标签?

这似乎不是hallo-js WYSIWYG问题或设置,而是某种我似乎无法找到的配置或其他设置。

BTW ..我使用的是Wagtail 1.13.1(使用默认的Hallo编辑器),Django 1.11和MySQL作为数据库。

为了解决我的问题,我正在使用此代码。

<em>

但应该有更好,更有效的方式。

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,并在Wagtail Slack支持渠道上提出了要求。我得到了register a new rich text feature的建议。该文档显示了删除线示例。这是粗体(strong)和斜体(em):

import wagtail.admin.rich_text.editors.draftail.features as draftail_features
from wagtail.admin.rich_text.converters.html_to_contentstate import (
    InlineStyleElementHandler,
)


@hooks.register('register_rich_text_features')
def register_strong_feature(features):
    """
    Registering the `strong` feature. It will render bold text with `strong` tag.
    Default Wagtail uses the `b` tag.
    """
    feature_name = 'strong'
    type_ = 'BOLD'
    tag = 'strong'

    # Configure how Draftail handles the feature in its toolbar.
    control = {
        'type': type_,
        'icon': 'bold',
        'description': 'Bold',
    }

    # Call register_editor_plugin to register the configuration for Draftail.
    features.register_editor_plugin(
        'draftail', feature_name, draftail_features.InlineStyleFeature(control)
    )

    # Configure the content transform from the DB to the editor and back.
    db_conversion = {
        'from_database_format': {tag: InlineStyleElementHandler(type_)},
        'to_database_format': {'style_map': {type_: tag}},
    }

    # Call register_converter_rule to register the content transformation conversion.
    features.register_converter_rule('contentstate', feature_name, db_conversion)

以及带有<em>标签的斜体:

@hooks.register('register_rich_text_features')
def register_em_feature(features):
    """
    Registering the `em` feature. It will render italic text with `em` tag.
    Default Wagtail uses the `i` tag.
    """
    feature_name = 'em'
    type_ = 'ITALIC'
    tag = 'em'

    control = {
        'type': type_,
        'icon': 'italic',
        'description': 'Italic',
    }

    features.register_editor_plugin(
        'draftail', feature_name, draftail_features.InlineStyleFeature(control)
    )

    db_conversion = {
        'from_database_format': {tag: InlineStyleElementHandler(type_)},
        'to_database_format': {'style_map': {type_: tag}},
    }

    features.register_converter_rule('contentstate', feature_name, db_conversion)

在您的富文本字段中指定功能。不要忘记删除旧的“粗体”和“斜体”:

from wagtail.core.fields import RichTextField

class FooPage(Page):
    body = RichTextField(features=['strong', 'em'])