Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  157] [ 2]  / answers: 1 / hits: 22990  / 11 Years ago, mon, november 11, 2013, 12:00:00

I have a heavily customized Django admin where it's very simple to load a custom JS file for each of my ModelAdmins:



class MyModelAdmin(admin.ModelAdmin):
class Media:
js = ('js/admin/mymodel.js',)


But how do I do this for the admin homepage, where all my admin models are listed?



Update #1: amending my question, since the solutions below aren't that useful if I cannot efficiently include Django's jQuery. So, how do I include Django's jQuery in the JS file? If I wrap my code with (as I do in my other ModelAdmin JS files):



(function ($) {
// my code here...
})(django.jQuery);


I get the following error:




ReferenceError: django is not defined.




Thanks.



Update #2: I was able to include Django's jQuery successfully by following this answer: https://stackoverflow.com/a/10584539/585783


More From » python

 Answers
26

You can override templates/admin/index.html and add the JavaScript in the block extrahead:



{% extends admin/index.html %}

{% block extrahead %}
{{ block.super }}
# add a <script> tag here with your JavaScript
{% endblock %}

[#74353] Sunday, November 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
irvingcarloe

Total Points: 677
Total Questions: 109
Total Answers: 96

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
irvingcarloe questions
Wed, Mar 31, 21, 00:00, 3 Years ago
Tue, Aug 4, 20, 00:00, 4 Years ago
Fri, Jul 3, 20, 00:00, 4 Years ago
;