上传前在鹡鸰中验证图像分辨率



我目前正在 Wagtail 中创建一个页面,该页面将允许客户端创建一个具有标题、正文和图像的新服务。我要问的是,如何在上传图像之前在图像上添加验证器以检查图像是否大于特定大小。

因此,客户端无法将小图像上传或选择到标题中,我可以限制它,以便该图像的最小分辨率需要为 500x500。

我四处摸索,找不到任何类似的东西。我在这里找到了 1 段代码,但是当我尝试在我的代码中使用它时,它给了我一个错误:

Field 'id' expected a number but got <Image: industrial.jpg>

这是我的页面模型:

class WhatWeDoPage(Page):
"""
The "What We Do" Page or the Services page. This will be the page where
we showing the services GR-Gear is doing.
"""
template = 'home/services.html'
services = StreamField([
('services', blocks.StructBlock([
('title', blocks.CharBlock()),
('body', blocks.RichTextBlock()),
('image', ImageChooserBlock(required=False, validators=[ImageValidator(width=500, height=500)]))
], icone='user'))
], blank=True)
content_panels = [
FieldPanel('title', classname="full title"),
StreamFieldPanel('services'),
]

任何帮助或协助将不胜感激

假设您使用的是最新版本的 Wagtail(截至撰写本文时为 2.9(,您的代码运行良好。有问题的错误发生在ImageValidator本身,我为解决这个问题所做的是更改__call__()下的这一行:

img = Image.objects.get(id=image)

对此:

img = Image.objects.get(id=image.id)

希望这有帮助!

从鹡鸰 2.9 开始也有WAGTAILIMAGES_MAX_IMAGE_PIXELS。只需添加到您的 settings.py

WAGTAILIMAGES_MAX_IMAGE_PIXELS = 500x500

见 https://docs.wagtail.io/en/stable/reference/settings.html?highlight=WAGTAILIMAGES_MAX_IMAGE_PIXELS#images

最新更新