自定义富文本字段中的图像

Customizing images in richtext fields

我在 this and this 的帮助下向自定义图像模型添加了标题字段。如何在富文本字段中的图像中显示标题字段?

这可以通过 defining a custom image format - in this case, you'd need to create a subclass of Format that overrides the image_to_html method. There's an example of this here: https://github.com/torchbox/verdant-rca/blob/d9ede994dbd1ef68eaa159ec930fd89a351c1329/django-verdant/rca/image_formats.py#L4-L25

但是,我强烈建议使用 StreamField 代替这种混合内容。带标题的图像位于富文本字段中实用内容的上端,并且行为可能有点小问题(例如,很难在位于字段末尾的图像之后或位于开头)。它也不适合内容和表示的分离。使用 StreamField,您的文本和图像在管理中显示为不同的 objects,并且在模板中您可以完全控制用于它们的 HTML。