You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级至Wagtail 5.1后出现cannot pickle '_io.BufferedReader' object错误

Wagtail 5.1 升级后{% image %}标签缓存报错解决方案

环境信息

  • Python 3.8
  • Wagtail 5.1
  • Ubuntu 20.04
  • 使用pipenv固定所有依赖

问题描述

站点从Wagtail 4.0逐步升级至5.0均正常运行,但升级到5.1后,后台与前台模板中的{% image ... %}标签全部报错,相关代码在5.0版本可正常工作,所有图片均引用自数据库记录。

后台模板代码

{% extends "wagtailadmin/base.html" %}
    {% load wagtailimages_tags %}
    
    {% block branding_logo %}
        {% image settings.main.SiteSettings.logo_admin width-150 %}
    {% endblock %}

首页模板代码

{% image settings.main.SiteSettings.logo_admin height-85 alt="Home" title="Home" style="position:relative; margin-bottom:-4px" %}

疑问

怀疑错误与Wagtail 5.1升级注意事项中「图片渲染版本(Image renditions)现已默认缓存」有关,由于站点流量极低,无需图片缓存,不想配置缓存。需明确:

  • Wagtail是否强制要求使用缓存?
  • 是否为Wagtail代码本身的错误?
  • 有无无需配置缓存即可解决错误的方法?

错误回溯

return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/venv/lib/python3.8/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/venv/lib/python3.8/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
  File "/venv/lib/python3.8/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/venv/lib/python3.8/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/venv/lib/python3.8/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/venv/lib/python3.8/site-packages/wagtail/images/templatetags/wagtailimages_tags.py", line 129, in render
    rendition = get_rendition_or_not_found(
  File "/venv/lib/python3.8/site-packages/wagtail/images/shortcuts.py", line 13, in get_rendition_or_not_found
    return image.get_rendition(specs)
  File "/venv/lib/python3.8/site-packages/wagtail/images/models.py", line 455, in get_rendition
    Rendition.cache_backend.set(cache_key, rendition)
  File "/venv/lib/python3.8/site-packages/django/core/cache/backends/locmem.py", line 53, in set
    pickled = pickle.dumps(value, self.pickle_protocol)

Exception Type: TypeError at /admin/
Exception Value: cannot pickle '_io.BufferedReader' object

解决方案

1. 直接关闭图片渲染版本缓存

Wagtail并未强制要求使用缓存,可通过修改项目settings.py禁用图片渲染版本缓存:

WAGTAILIMAGES_RENDITION_CACHE = None

该配置会让Wagtail跳过缓存逻辑,直接生成图片渲染版本,完全适配低流量站点需求。

2. 修复缓存序列化问题(可选)

若后续需要开启缓存,当前报错是因为默认的locmem缓存无法序列化_io.BufferedReader对象。可改用支持复杂对象序列化的缓存后端,比如文件缓存:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': '/var/tmp/django_cache',
    }
}

但对于低流量站点,直接关闭缓存是更简单的方案。

3. 临时降级至Wagtail 5.0(应急方案)

若上述方法暂时不生效,可先降级回Wagtail 5.0,待问题修复后再升级:

pipenv install wagtail==5.0

内容的提问来源于stack exchange,提问作者Dan Swain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 19:45:05