0

While following the Wagtail 5.x documentation on ModelAdmin customization, I have created a Wagtail ModelAdmin definition as follows:

class PersonModelAdmin(ModelAdmin):
    model = Person
    menu_icon = "user"
    menu_label = "People"
    list_per_page = 10
    ordering = ["family_name", "given_name"]
    list_display = ("family_name", "given_name")
    empty_value_display = "-"
    search_fields = ("given_name", "family_name")

    # TODO: determine why the following code does not work
    # The goal is to load the js file in the admin
    # only when the Person model is being edited
    # rather than globally as we do at the end of this file
    form_view_extra_js = [
        "js/contact/person_url_slug.js",
    ]

With that configuration when loading the Create New Person page in the Wagtail Admin UI, there is no HTTP request to fetch the file. E.g., there is no 404 response for a mislocated file, just no request.

However, with the following configuration in the same wagtail_hooks.py, I am able to load the file:

@hooks.register("insert_editor_js")  # type: ignore
def editor_js() -> SafeString:
    js_files = [
        "js/contact/person_url_slug.js",
    ]
    js_includes = format_html_join(
        "\n",
        '<script src="{0}{1}"></script>',
        ((settings.STATIC_URL, filename) for filename in js_files),
    )
    return js_includes

The above approach is much more verbose and has the effect of loading the JS file on all edit forms, which is not desirable.

How can I troubleshoot why the form_view_extra_js doesn't seem to work? E.g., is there some way I can get some debug information such as whether the file can't be found by Wagtail or if there is some other misconfiguration?

I've got DEBUG = True in my development environment, but don't see any errors when loading the Person create/edit forms.

I was expecting that the JS file would be loaded when viewing the Person create/edit forms.

Brylie Christopher Oxley
  • 1,684
  • 1
  • 17
  • 34
  • Given the lack of Google search results for "wagtail form_view_extra_js not loading script file", I suspect there is something wrong with my set-up. However, I can't get much debugging detail from Wagtail to troubleshoot the issue. – Brylie Christopher Oxley Jul 14 '23 at 12:16

0 Answers0