我目前正在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)
希望这对你有帮助!