Adding a jQuery script to the Django admin interface

后端 未结 1 1412
太阳男子
太阳男子 2020-12-04 18:22

I\'m gonna slightly simplify the situation. Let\'s say I\'ve got a model called Lab.

from django.db import models

class Lab(models.Model):
    acronym = mod         


        
相关标签:
1条回答
  • 2020-12-04 18:54

    To add media to the admin you can simply add it to the meta class Media of your admin class, e.g.:

    admin.py

    class FooAdmin(admin.ModelAdmin):
        # regular stuff
        class Media:
            js = (
                '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', # jquery
                'js/myscript.js',       # project static folder
                'app/js/myscript.js',   # app static folder
            )
    
    admin.site.register(Foo, FooAdmin)
    

    Mind the trailing comma if you only have one file included, as it has to be a tuple. You can also opt in css this way.

    The admin already has (an older version) of jquery included. To shortcut it for usage add this to the top of the 'myscript' file:

    if (!$) {
        $ = django.jQuery;
    }
    

    To solve your problem, I would extend the admin. You can add a js event to any DOM node to trigger an ajax call in your myscript file to the correct admin view for handling.

    0 讨论(0)
提交回复
热议问题