Andrei Zhamoida
Andrei Zhamoida

Reputation: 1464

How correctly use a hook in a Wagtail admin?

I'm trying to insert my own css into a wagtail admin page.

Сonsidering the answer https://groups.google.com/forum/#!topic/wagtail/DYeTygB_F-8 I use hook insert_editor_css. I've created wagtail_hooks.py in my app folder and added code below:

from django.utils.html import format_html
from django.contrib.staticfiles.templatetags.staticfiles import static

from wagtail.wagtailcore import hooks


@hooks.register('insert_editor_css')
def editor_css():
    return format_html('<link rel="stylesheet" href="{}">', static('css/admin.css'))

Towards to docs it should be executed but no css file inserted or even attempt to do it(errors or exceptions). I guess(maybe i'm wrong) wagtail_hooks.py is not being processed.

Could anybody throw me some tips? Thanks in advance.

Upvotes: 2

Views: 2876

Answers (1)

m1kola
m1kola

Reputation: 696

You need to:

  1. Ensure that your app was added to the INSTALLED_APPS setting.
  2. Ensure that your project imports wagtail_hooks.py. You can just put print somewhere on the module level or add a break point to hook function definition.
  3. Ensure that you use proper hook:

    insert_editor_css and insert_editor_js used to add extra css or js only to the page editor interface. So your css/admin.css should appear on a page create or edit screen.

    If you want to add extra css or js to all admin pages you need to use insert_global_admin_css or insert_global_admin_js.

Upvotes: 5

Related Questions