0

How can I call external javascript in my default admin html page in Django.

I know that using MEDIA class I can do this in admin.py file.

Here is the my try:

  class PlayerAdmin(admin.ModelAdmin):
      class Media:
        js = ('/static_files/js/adminsettings.js')

But I am not getting data written in the JavaScript file.

1 Answer 1

1

You need to use a path relative to STATIC_URL (or MEDIA_URL), in your example this may work:

  class PlayerAdmin(admin.ModelAdmin):
      class Media:
        js = ('js/adminsettings.js',)

Also, not that the js attribute is a tuple in my example (it can be any iterable, a list, set, etc.)

An alternative is overriding admin templates, which can give you more flexibility, and allow you to apply the same javascript to many models easily.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.