(images_overview)= # 图片概览 本页概述在你的 Wagtail 项目中使用 `'wagtail.images'` 应用的基础知识。 ## 在 `INSTALLED_APPS` 中包含 `'wagtail.images'` 要使用 `wagtail.images` 应用,你需要把它包含在 Django 项目设置的 `INSTALLED_APPS` 列表中。像这样把它加入列表即可: ```python # settings.py INSTALLED_APPS = [ # ... "wagtail.images", # ... ] ``` ## 在页面中使用图片 要给 Wagtail 页面添加图片,请在页面模型中为图片模型添加一个 `ForeignKey`,并用 [`FieldPanel`](wagtail.admin.panels.FieldPanel) 暴露它。 下面是一个例子: ```python # models.py from django.db import models from wagtail.admin.panels import FieldPanel from wagtail.models import Page class YourPage(Page): # ... image = models.ForeignKey( "wagtailimages.Image", null=True, blank=True, on_delete=models.SET_NULL, related_name="+", ) content_panels = Page.content_panels + [ # ... FieldPanel("image"), ] ``` `on_delete=models.SET_NULL` 让页面在图片从图片库中被删除后仍然可用,而 `related_name="+"` 则避免给图片模型添加反向访问器(这个访问器很少有用)。 如果你的项目使用自定义图片模型,请改用 `get_image_model_string()` 引用它,而不是 `"wagtailimages.Image"`。见 [](custom_image_model_referring_to_image_model)。 ## 在模板中使用图片 Wagtail 提供了一组有明确主张的[图片模板标签](image_tag),用于自动把源图片转换成[多种格式](multiple_formats)和[多种尺寸](responsive_images)。这些生成的图片被称为 _rendition_,即为了尺寸、格式、质量或其他变换而重新处理过的图片版本。 例如,[`picture` 模板标签](multiple_formats)会自动创建一个包含多种格式和尺寸的 `` 元素,让浏览器选择它更偏好的那个。例如: ```html+django {% load wagtailimages_tags %} {% picture myimage format-{avif,webp,jpeg} width-1000 %} ``` 关于 rendition 选项、替代文本的处理,以及在 Python 中而不是模板中生成 rendition,见 [](image_tag)和 [](image_renditions)。 ## 在 `RichTextField` 中使用图片 编辑可以在 [`RichTextField`](rich_text_field) 中插入图片。`image` 功能默认启用,所以无需配置。 如果你传入显式的 `features` 列表,请包含 `"image"` 以保持它可用: ```python # models.py from wagtail.fields import RichTextField class BlogPage(Page): # ...other fields body = RichTextField(blank=True, features=["bold", "italic", "ol", "image"]) panels = [ # ...other panels FieldPanel("body"), ] ``` 完整的特性清单见 [](rich_text_features),关于定制图片的渲染方式见 [](changing_rich_text_representation)。 ## 在 `StreamField` 中使用图片 `StreamField` 提供了一种适合不遵循固定结构页面的内容编辑模型。使用 [`ImageBlock`](streamfield_imageblock) 把图片加入 `StreamField`: ```python # models.py from wagtail.fields import StreamField from wagtail.images.blocks import ImageBlock class BlogPage(Page): # ... other fields body = StreamField( [("image", ImageBlock())], blank=True, ) panels = [ # ... other panels FieldPanel("body"), ] ``` 在 `blog_page.html` 中,像渲染其他任何图片那样渲染该块: ```html+django {% load wagtailimages_tags %} {% for block in page.body %} {% picture block.value format-{avif,webp,jpeg} width-800 %} {% endfor %} ``` `ImageBlock` 让编辑可以把图片标记为装饰性的,或给它指定贴合上下文的替代文本,而 `ImageChooserBlock` 做不到这一点。见我们的内容建模指引中的[图片的替代文本](content_modeling)。 ## 使用图片与集合 Wagtail 中的图片可以在[集合](https://guide.wagtail.org/en/how-to-guides/manage-collections/)中组织。集合提供了一种把相关图片分组的方式,也可以用来在你自己的视图中过滤图片: ```python # models.py from wagtail.images import get_image_model class GalleryPage(Page): collection = models.ForeignKey( "wagtailcore.Collection", null=True, blank=True, on_delete=models.SET_NULL, related_name="+", verbose_name="Image collection", ) content_panels = Page.content_panels + [ FieldPanel("collection"), ] def get_context(self, request): context = super().get_context(request) context["images"] = get_image_model().objects.filter(collection=self.collection) return context ``` 下面是一个渲染该集合的示例模板: ```html+django {% load wagtailimages_tags %} {% block content %} {% for img in images %} {% image img width-400 %} {% endfor %} {% endblock %} ``` ## 把图片设为私有 如果你想限制对某些图片的访问,可以把它们放入[私有集合](private_collections)。 私有集合不公开可访问,其内容只对拥有相应权限的用户可用。 ## 在 Wagtail 之外提供图片 对于服务端渲染的站点,rendition 通常由 Wagtail 的图片模板标签或 `get_rendition()` 生成。如果外部系统(例如移动应用或无头前端)需要按 URL 请求图片版本,Wagtail 提供了一个动态服务视图。见 [](using_images_outside_wagtail)。 ## API 访问 图片也可以通过 Wagtail 内置的 API 支持访问。你可以直接[把图片作为页面数据的一部分访问](api_v2_images)。 要直接访问图片,你也可以[为 v2 API 配置图片端点](api_v2_configure_endpoints)。 只要启用了 v3 API,[v3 图片 API 端点](api_v3_images)也可用,并提供诸如“写”操作等更高级的功能。