如何在 Wagtail 管理员中正确使用钩子?

And*_*ida 2 python django wagtail

我正在尝试将我自己的 css 插入到 wagtail 管理页面中。

\n\n

\xd0\xa1onsidering 答案https://groups.google.com/forum/#!topic/wagtail/DYeTygB_F-8我使用 hook insert_editor_css。我在我的应用程序文件夹中创建wagtail_hooks.py并添加了以下代码:

\n\n
from django.utils.html import format_html\nfrom django.contrib.staticfiles.templatetags.staticfiles import static\n\nfrom wagtail.wagtailcore import hooks\n\n\n@hooks.register(\'insert_editor_css\')\ndef editor_css():\n    return format_html(\'<link rel="stylesheet" href="{}">\', static(\'css/admin.css\'))\n
Run Code Online (Sandbox Code Playgroud)\n\n

对于文档,它应该被执行,但没有插入 css 文件,甚至没有尝试执行它(错误或异常)。我猜(也许我错了)wagtail_hooks.py没有被处理。

\n\n

有人能给我一些建议吗?提前致谢。

\n

m1k*_*ola 5

你需要:

  1. 确保您的应用程序已添加到INSTALLED_APPS设置中。
  2. 确保您的项目导入wagtail_hooks.py. 您可以将其放在print模块级别的某个位置,或者在钩子函数定义中添加断点。
  3. 确保使用正确的钩子:

    insert_editor_css并用于向页面编辑器界面insert_editor_js添加额外的 css 或 js 。因此,您应该出现在页面创建或编辑屏幕上。css/admin.css

    如果您想向所有管理页面添加额外的 css 或 js,您需要使用insert_global_admin_cssinsert_global_admin_js.