上传时生成标题

上传文件(文档)时,Wagtail 取文件名,去掉扩展名,然后填入标题字段。本节介绍如何定制这种从文件名到标题的转换。

从文件名到标题的转换用于单文件组件、多文件上传组件,以及选择器弹窗中。

你也可以为图片定制同样的行为。

你可以用 JavaScript 事件监听器监听 'wagtail:documents-upload' 事件,来定制这个标题的解析结果。

向编辑器添加 JavaScript 的最简单方式是通过 insert_global_admin_js hook。不过任何添加事件监听器的 JavaScript 都可以工作。

DOM 事件

要监听的事件名是 'wagtail:documents-upload'。它会在文档上传的 form 上派发。事件的 detail 属性包含:

  • data - 一个包含要使用的 title 的对象。它是去掉扩展名的文件名。

  • maxTitleLength - 一个整数(或 null),表示 Document 模型标题字段的最大长度。

  • filename - 未去掉扩展名的原始文件名。

要修改生成的 Document 标题,访问并更新 event.detail.data.title,不需要返回值。

对单份文档上传,这个自定义事件只在标题还没有值时才运行,这样我们不会覆盖用户已经输入的内容。

调用 event.preventDefault() 可以阻止默认行为。对单个上传页面或弹窗,这样不会向标题预填任何值。对多份上传,这样会避免提交任何标题,只使用文件名作为标题(带文件扩展名),因为保存文档需要标题。

事件会“冒泡”上传,因此你可以简单地添加一个全局的 document 监听器来捕获所有这些事件,也可以按需收窄监听器或处理逻辑的作用域,确保只在特定场景下调整标题。

关于自定义 JavaScript 事件的更多信息见 MDN。

代码示例

对下面的每个示例,请在你的应用静态目录(例如 static/js/)中创建指定的外部 JavaScript 文件,并在 wagtail_hooks.py 文件中引用它。

把文件扩展名加到标题开头

# wagtail_hooks.py
from django.templatetags.static import static
from django.utils.html import format_html

from wagtail import hooks


@hooks.register("insert_global_admin_js")
def get_global_admin_js():
    script_url = static("js/title_with_extension.js")
    return format_html('<script src="{}"></script>', script_url)
// title_with_extension.js
window.addEventListener('DOMContentLoaded', function () {
    document.addEventListener('wagtail:documents-upload', function (event) {
        const extension = (event.detail.filename.match(
            /\.([^.]*?)(?=\?|#|$)/,
        ) || [''])[1];
        const newTitle = `(${extension.toUpperCase()}) ${event.detail.data.title || ''}`;
        event.detail.data.title = newTitle;
    });
});

只在页面编辑器上更改生成的标题,去掉破折号 / 下划线

改用 insert_editor_js hook,这样该脚本只会在页面编辑器上运行,不会在 Document 上运行。

# wagtail_hooks.py
from django.templatetags.static import static
from django.utils.html import format_html

from wagtail import hooks


@hooks.register("insert_editor_js")
def get_editor_js():
    script_url = static("js/remove_dashes_underscores.js")
    return format_html('<script src="{}"></script>', script_url)
// remove_dashes_underscores.js
window.addEventListener('DOMContentLoaded', function () {
    document.addEventListener('wagtail:documents-upload', function (event) {
        // 把破折号 / 下划线替换为空格
        const newTitle = (event.detail.data.title || '').replace(
            /(\s|_|-)/g,
            ' ',
        );
        event.detail.data.title = newTitle;
    });
});

停止根据文件名预填标题

# wagtail_hooks.py
from django.templatetags.static import static
from django.utils.html import format_html

from wagtail import hooks


@hooks.register("insert_global_admin_js")
def insert_stop_prefill_js():
    script_url = static("js/stop_title_prefill.js")
    return format_html('<script src="{}"></script>', script_url)

把下面的代码保存为 static/js/stop_title_prefill.js

// stop_title_prefill.js
window.addEventListener('DOMContentLoaded', function () {
    document.addEventListener('wagtail:documents-upload', function (event) {
        // 单文件上传时会停止标题预填
        // 多文件上传时会把标题设为文件名(带扩展名)
        event.preventDefault();
    });
});