Joseph Tura
Joseph Tura

Reputation: 6360

Django: django-admin-tools: adding custom js

I have installed django-admin-tools and created a dashboard.py in my project folder.

Inside this file I have specified a media class:

#myproject/dashboard.py 
class Media: 
        css = ('',) 
        js = ('http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/ 
jquery.min.js',) 

In my settings I have:

#settings.py
# admin_tools 
ADMIN_TOOLS_INDEX_DASHBOARD = 
'myproject.dashboard.CustomIndexDashboard' 
ADMIN_TOOLS_APP_INDEX_DASHBOARD = 
'myproject.dashboard.CustomAppIndexDashboard' 

And my URLs are configured as follows:

#urls.py
... 
urlpatterns+= patterns('', 
        url(r'^admin_tools/', include('admin_tools.urls')), 
        url(r'^admin/', admin.site.urls), 

        url(r'', include('feincms.urls')), 

) 

Anyone see any glaring mistakes? I don't see the jquery file being downloaded in firebug. I assume jquery is also part of admin_tools, but this error message seems to indicate it is not?

Uncaught TypeError: Property '$' of object [object DOMWindow] is not a 
function 

Any help is appreciated.

Upvotes: 0

Views: 1289

Answers (2)

Sebastian Thomas
Sebastian Thomas

Reputation: 1324

I think this is probably because the jQuery function has been renamed in the django admin to avoid conflicts.
If you can see the jquery file being loaded in the view-source, and typing $ in the console produces that error, then try django.jQuery
If you want to use $ you need to do something like $ = django.jQuery, and then at the bottom of you script, put it back to django.jQuery
See https://github.com/philippbosch/django-geoposition/blob/master/geoposition/static/geoposition/geoposition.js as an eg.

Upvotes: 1

Dirk Eschler
Dirk Eschler

Reputation: 2569

You are right, jquery is already included by admin_tools. Unless you need a newer version, it's probably better to use the included one. Or doesn't it load even without the Media class?

Problematic in your setup is that you load jquery from an external host. I would also expect this to work, however, if you look at the dashboard.html template in admin_tools which injects the files, you will notice that it prepends {{ media_url }} to each js file. The result is an invalid include like '/media/http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'. This is obviously an issue of admin_tools and i would file a bug report.

As a workaround you might remove the Media class and override dashboard.html to include your external files.

Upvotes: 0

Related Questions