1

I know that I am allowed to add customized JS into django admin BUT can I just add something like <script>var a = 'b';</script> into django admin?

Now I am using something like

meta Media:
     js = ['/site_media/choice.js']

the reason I dont want to use the above is, I have a script which is like the above and the script will run differently in different admin model pages.

for example in the above choice.js I have a function run()

in model admin A I will want this page to use run('abc') but in model admin B I want this page to use run('eee')

if adding js instead of just typing out script, that means I will have to create quite a lot js

Can someone give me a hand of this? (currently using django 1.10.5)

Edited: Sorry my bad, I did try using the template first and few other ways but none worked

Dora
  • 6,776
  • 14
  • 51
  • 99
  • Possible duplicate of [How to override and extend basic Django admin templates?](http://stackoverflow.com/questions/6583877/how-to-override-and-extend-basic-django-admin-templates) – e4c5 Feb 27 '17 at 13:29
  • @e4c5 tried that already, before I posted this :( – Dora Feb 28 '17 at 07:35
  • then you should explain that in your question. – e4c5 Feb 28 '17 at 07:36

1 Answers1

1
class CustomAdmin(ModelAdmin):
    class Media:
        css = []
        js = ['sites/javascriptfile.js']

It is the Correct code to add js files or css files in django admin just check the static path in settings.py file and path should be relative to static path.

Ok i Got it, you can check Url in javascript function if it is for 'abc' you can run run('abc') in javascript if condition and else another one.

You can check url by

$(document).ready(function () {
    if(window.location.href.indexOf("add") > -1) {
       // This is Add Page
       run('abc')
    }
    else if(window.location.href.indexOf("change") > -1) {
       // This is Change Page
       run('def')
    }
});

There are many more methods to check URL.

Filipon
  • 111
  • 4