Use javascript, css in admin

Posted: June 2nd, 2010 | Author: Davo | Filed under: Django | Tags: , , , | 1 Comment »

Ever wanted to use your own JavaScript OR CSS inside the built in Django admin?
Well… it is possible!

All you need to do is the following:
1)
Add a admin.py (if you don’t already have yet…) for your app

from django.db import models
from django.contrib import admin

from myapp.hotels.models import Hotel

class HotelAdmin(admin.ModelAdmin):
    ...
    class Media:
        js = ("/media/javascript/yourjs.js",)

admin.site.register(Hotel, HotelAdmin)

2) Create a file yourjs.js under /media/javascript

(function($) {
    $(document).ready(function($) {
         // you can now use jquery / javascript here...
         alert('It worked.');
    });
})(django.jQuery);

3) Open the admin for your application and you should see the ‘It worked’ message.


One Comment on “Use javascript, css in admin”

  1. 1 Forrest Aldridge said at 2:02 pm on March 6th, 2011:

    God bless you, sir!

    I tore my hair out for hours on this problem until coming across this post that explained that my own scripts and any jQuery plugins I am using needed to be namespaced under django.jQuery.

    Thank you so very much; do you have a donate link anywhere? ;-)


Leave a Reply