Search code examples
javascriptdjangodjango-formsdjango-templatesdjango-admin

How to override django admin templates to add custom javascript?


I have a model that I need to add some custom javascript processing to its admin form.

I have tried an implementation via the following guide: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#overriding-admin-templates

So I created my own change_form.html, and I overrode object-tools-items and put my js in there, but I'm not seeing it when I go to the change form. Then, just as a test, I put it directly into the real django change_form.html, but still nothing.

Then to see if that template is being used, I changed it - added data, created syntax errors, but still, it had no effect. So it seems like that template isn't being used at all. I grepped for change_form.html to see where it's rendered from, and I found it in contrib/admin/options.py:render_change_form(), so I set a breakpoint there, but it was never hit. But the HTML sure looks like it came from that template.

Can anyone give me some direction here please?


Solution

  • You do not need to override admin templates to add your custom javascript to admin pages.

    You can add your assets like this:

    https://docs.djangoproject.com/en/dev/topics/forms/media/#assets-as-a-static-definition

    And then you just need to override your forms that admin site uses.