Wagtail 将 RawHTMLBlock 添加到模型中的字段
Wagtail add RawHTMLBlock to field in model
我正在尝试在我的模型中将字段类型添加为 RawHTMLBlock
我做到了
from wagtail.images.blocks import ImageChooserBlock
from wagtail.core import blocks
from wagtail.core.fields import RichTextField, StreamField
from wagtail.admin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel, StreamFieldPanel, FieldRowPanel, PageChooserPanel
from modelcluster.fields import ParentalKey
from wagtail.images.models import Image
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.images.widgets import AdminImageChooser
@register_snippet
class DefaultHeader(models.Model):
title_html = models.CharField(max_length=100, null=True, blank=True)
text_message = RichTextField(null=True, blank=True)
code_text = blocks.RawHTMLBlock(null=True, blank=True)
background = models.ForeignKey('wagtailimages.Image',
related_name='+',
null=True,
blank=True,
verbose_name=_("Background"),
on_delete=models.SET_NULL)
panels = [
FieldPanel("title_html"),
FieldPanel("text_message"),
FieldPanel("code_text"),
ImageChooserPanel("background", classname="col12"),
]
所有字段都是在 makemigrations 之后添加的,除了 code_text 该字段未添加
在我的管理页面中,我有 title_html; text_message;背景。但不是 code_text
块在 StreamField 之外无效。要在模型字段中实现与 RawHTMLBlock 相同的功能,将其定义为 TextField:
code_text = models.TextField(blank=True)
然后,在模板上输出时,使用 Django's |safe
filter 禁用作为标准应用的 HTML 转义:
{{ header.code_text|safe }}
我正在尝试在我的模型中将字段类型添加为 RawHTMLBlock
我做到了
from wagtail.images.blocks import ImageChooserBlock
from wagtail.core import blocks
from wagtail.core.fields import RichTextField, StreamField
from wagtail.admin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel, StreamFieldPanel, FieldRowPanel, PageChooserPanel
from modelcluster.fields import ParentalKey
from wagtail.images.models import Image
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.images.widgets import AdminImageChooser
@register_snippet
class DefaultHeader(models.Model):
title_html = models.CharField(max_length=100, null=True, blank=True)
text_message = RichTextField(null=True, blank=True)
code_text = blocks.RawHTMLBlock(null=True, blank=True)
background = models.ForeignKey('wagtailimages.Image',
related_name='+',
null=True,
blank=True,
verbose_name=_("Background"),
on_delete=models.SET_NULL)
panels = [
FieldPanel("title_html"),
FieldPanel("text_message"),
FieldPanel("code_text"),
ImageChooserPanel("background", classname="col12"),
]
所有字段都是在 makemigrations 之后添加的,除了 code_text 该字段未添加 在我的管理页面中,我有 title_html; text_message;背景。但不是 code_text
块在 StreamField 之外无效。要在模型字段中实现与 RawHTMLBlock 相同的功能,将其定义为 TextField:
code_text = models.TextField(blank=True)
然后,在模板上输出时,使用 Django's |safe
filter 禁用作为标准应用的 HTML 转义:
{{ header.code_text|safe }}