alekwisnia
alekwisnia

Reputation: 2354

Best way to alter jquery version in Django admin

What is the best way to load jQuery on all Django admin pages. I would like to load it from Google CDN, at the moment I have to remember to load it on all my admin pages separatelly.

Upvotes: 0

Views: 3026

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239430

Django comes with jQuery by default. If you just need jQuery and don't mind using the same version Django uses (a little outdated), you can just wrap your code with:

(function($){
    // your code, use `$` as normal
})(django.jQuery);

However, if you want a newer version of jQuery, it is not recommended to actually change the version of jQuery Django uses. Thankfully, since Django namespaces its version of jQuery by default (as django.jQuery), you can load your own version as normal without causing any conflicts.

Create a file at <project_root>/templates/admin/base_site.html. Copy and paste the code from the default base_site.html (minimal amount of code here, so won't affect upgrades), and add the following block:

{% block extrahead %}
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
{% endblock %}

Of course, you can change that to any version of jQuery you want.

Upvotes: 5

Alex
Alex

Reputation: 9041

From here - http://www.djangobook.com/en/1.0/chapter06/

The admin site finds the “Django administration” header by looking for the template admin/base_site.html. By default, this template lives in the Django admin template directory, django/contrib/admin/templates, which you can find by looking in your Python site-packages directory, or wherever Django was installed. To customize this base_site.html template, copy that template into an admin subdirectory of whichever directory you’re using in TEMPLATE_DIRS. For example, if your TEMPLATE_DIRS includes "/home/mytemplates", then copy django/contrib/admin/templates/admin/base_site.html to /home/mytemplates/admin/base_site.html. Don’t forget that admin subdirectory. 3

Then, just edit the new admin/base_site.html file to replace the generic Django text with your own site’s name as you see fit.

So just add/edit the jquery include in this custiomised file.

Hope that helps...

Upvotes: 0

Related Questions