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

3
  • Possible duplicate of How to override and extend basic Django admin templates? Commented Feb 27, 2017 at 13:29
  • @e4c5 tried that already, before I posted this :( Commented Feb 28, 2017 at 7:35
  • then you should explain that in your question. Commented Feb 28, 2017 at 7:36

1 Answer 1

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.

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

1 Comment

this is not what I meant

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.