5

I'm using django-debug-toolbar (newest version, v1.2.1) and I keep getting this error in the console:

Empty string passed to getElementById()

For this bit of code in jQuery (line 2757):

...
// HANDLE: $(#id)
} else {
elem = document.getElementById( match[2] );
...

Each link in the debug toolbar I click gives another occurrence of this warning.

When I comment out my main jQuery source file, debug toolbar works. Also, in the Django admin, it works fine, presumably because the Django admin uses a different method of fetching jQuery.

I tried @Carlton Gibson's answer but this doesn't solve my problem. I also have tried local jQuery and from a CDN and a few different versions to no avail...

I have tried different versions of jQuery and using all the possible settings offered in the docs of django-debug-toolbar. I'm stuck and I really like this tool and want it to work for me.

EDIT

Since I have received some additional comments and answers, I thought I'd make an edit to explain what I've tried. I tried all suggestions without success. However, currently, I'm seeing another error that I recognize from another post about RequireJS and django-debug-toolbar:

GET http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js [HTTP/1.1 304 Not Modified 133ms]
ReferenceError: jQuery is not defined localhost:8000:119
TypeError: djdt is undefined toolbar.js:297
Empty string passed to getElementById()

Even though jQuery is loaded and I'm telling django-debug-toolbar to use my version (with DEBUG_TOOLBAR_CONFIG = { 'JQUERY_URL': '', }), I'm still getting the errors and DjDT isn't working. This time, it's not even showing up because of the TypeError.

EDIT

Here is the chunk of base.html where the JS is imported. Keep in mind that this a snapshot and it has changed quite a bit.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js" defer></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js" defer></script>

I also have a back to top widget I'm using that might be the culprit. Since I've been using this code for so long, I never thought it was a problem but now I suspect it might be. Also, since the error was related to id and the script uses class I dismissed it. But when I comment it out, the DjDT works.

With the help of the posts herein, I got this issue resolved. Not sure how it relates to the id, but I had a conflict in a back to top widget I'm using with an a selector. I changed to the class and it's working. I tested in two projects and it seems to be fixed. Thanks for all the help!

nicorellius
  • 3,715
  • 4
  • 48
  • 79
  • Does your site/project happen to use RequireJS? – Michael B Nov 28 '14 at 09:07
  • Not intentionally... The only libraries used in the projects of concern are Bootstrap and jQuery... – nicorellius Nov 28 '14 at 17:40
  • The reason I mention this is that there is currently a bug report for it: https://github.com/django-debug-toolbar/django-debug-toolbar/issues/605. It suggests setting `DEBUG_TOOLBAR_CONFIG = {'JQUERY_URL': None}` then making sure to load jQuery before requireJS. – Michael B Nov 30 '14 at 09:24
  • Also, is there a possibility that I could ask that you show the part of your relevant base.html template where you load your CSS/JS for bootstrap/jQuery? – Michael B Nov 30 '14 at 09:50
  • Yeah, I saw that bug... See OQ for code... – nicorellius Nov 30 '14 at 16:02
  • I meant the actual HTML which loads the scripts in the `` in the HTML, not the Javascript code. – Michael B Nov 30 '14 at 18:38

2 Answers2

6

Since you've already got jQuery on the page you can tell the debug toolbar not to load it's version. Try adding this to your settings:

DEBUG_TOOLBAR_CONFIG = {
  'JQUERY_URL':'',
}

(See the config docs for a little more.)

Hopefully that's enough to solve your issue.

Carlton Gibson
  • 7,278
  • 2
  • 36
  • 46
  • Thanks for the suggestion. I added this to my local settings file and it definitely worked, eg, forced my version of jQuery to be used, but still no data showed when `debug_toolbar` links are clicked... Same error, `Empty string passed to getElementById()`. – nicorellius Jul 08 '14 at 22:28
  • Hmmm. Curious. > This time, I made some some changes in my code, thinking they wouldn't have an effect... All I can ask is "What did you do?" — Revert that and see if it works :) – Carlton Gibson Jul 09 '14 at 07:39
  • So this problem now has me banging my head against the wall... I now have multiple Django projects, a couple that are new, and none has a functioning `django-debug-toolbar` except one that doesn't have a version of jQuery loaded on the page. I've tried so many things that I came back here with a bounty. – nicorellius Nov 28 '14 at 02:20
  • @ Carlton Gibson - RE: backtracking to find where it went wrong. This didn't help as I tore apart that particular project looking for the problem. I moved on and since have started additional Django projects that I would expect to work out of the gate. The only common piece to all this is that I import jQuery close to the bottom the templates (before the closing `

    ` tag)... Although, I've tried in the `

    ` as well to no avail.

    – nicorellius Nov 28 '14 at 02:28
1

Not sure if this will work for you, but taking cue from this answer, try adding the following to your settings.py and see if it works

def show_toolbar(request):
    return True
SHOW_TOOLBAR_CALLBACK = show_toolbar

That will effectively remove all checks by debug toolbar to determine if it should or should not load itself; it will always just load.

Since you mention in the comment that you are importing jquery at the bottom of the </body> tag, make sure that all your tags a closed; there might be a missing tag in your template. Also, if you are putting the jquery script tag at the bottom for performance sakes, you can look into using async or defer.

Edit 1 : More suggestions based on comments

Are you using any extensions for Firefox or chrome etc? Check this. Also, make sure that if you are querying by '#' selector -- insure the selector is unique (as it should be) or you will end up with this error in FireFox.

My guess would be, that you have an element in your html with some id, which is used within the templates for the django-debug toolbar as well, and you are querying it with # selector.

Community
  • 1
  • 1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • Thanks for the suggestions. `django-debug-toolbar` actually loads fine, it's just that clicking on the panels don't work (and instead throw the error mentioned above). I tried your ideas to no avail (but the `defer` and `async` methods are good to use anyway). I just went through my template again and confirmed all tags are closed and everything looks good. It's truly bizarre, because as soon as I remove jQuery, `django-debug-toolbar` functions correctly. – nicorellius Nov 28 '14 at 17:44
  • @nicorellius More suggestions :). Are you using any extensions for Firefox or chrome etc? Check [this](http://stackoverflow.com/a/10790470/1860929). Also, make sure that if you are querying by '#' selector -- insure the selector is unique (as it should be) or you will end up with this error in ff ([link](http://stackoverflow.com/a/14060242/1860929)) – Anshul Goyal Nov 28 '14 at 17:58
  • Good thoughts (keep them coming)... I just checked this by deactivating all add-ons and extensions and testing FF again. No dice. Then, I went ahead and tested in Chrome and Opera and the DJDT did not work in any of these. Seriously, I'm about ready to spin up a VM with a vanilla set up to test this because it's driving me nuts. – nicorellius Nov 28 '14 at 18:08
  • 1
    @nicorellius my guess would be, that you have an element in your html with some id, which is used within the templates for the django-debug toolbar as well, and you are querying it with a `#` selector. – Anshul Goyal Nov 28 '14 at 18:09
  • That's a good thought. The thing is, this would have to be systematic in many of my projects, because DJDT isn't working in several projects. I'm now looking for IDs that might cause this conflict. Thanks. – nicorellius Nov 28 '14 at 18:20
  • @nicorellius find anything useful? – Anshul Goyal Dec 01 '14 at 02:57
  • 1
    I did... After your comment and Michael B's above, I started looking at a widget I built for back to top functionality. It doesn't use any ids but it does have a conflict I never realized before. An anchor instead of the class (why I used this selector I do not know). So I changed it and it seems to be working. Testing now. – nicorellius Dec 02 '14 at 17:09