3

(Note: the answer is a qualified 'yes' - it should work if not for my requirejs configuration issue - see my update at the end)

I'm looking into customizing the admin for a number of Models where savvy end-users are expected to do the maintenance.

I've seen a number of SO questions, such as How to override and extend basic Django admin templates?, on how to achieve this.

I expect that knowing which template files are being used by the admin at any particular point is key to customizing efficiently. So, I re-enabled the Django Debug Toolbar (hopefully wo requirejs side-effects this time).

The Django Debug Toolbar works and shows up in my apps' pages. But it doesn't show up on the admin pages. Is that to be expected?

enter image description here

Django (1.8.11)
django-debug-toolbar (1.4)

Why it's not working/Update:

I think I know what is happening. When looking at Firebug to see what CSS is involved with an admin page, I noticed that it was requesting debug toolbar CSS:

http://localhost:8000/static/debug_toolbar/css/toolbar.css

Which got me to think of requireJS incompatibility again. Sure enough, in the console, I see this error.

TypeError: $ is undefined http://localhost:8000/static/debug_toolbar/js/toolbar.js Line 297

So, again a requireJS-DJT glitch.

FYI, my Debug toolbar workaround for requireJS was (from https://github.com/django-debug-toolbar/django-debug-toolbar/issues/605):

settings.py

DEBUG_TOOLBAR_CONFIG = {
"JQUERY_URL": None,

}

and in my app's base template:

(this is the part that is missing from the admin pages)

{% block requirejs %}

//as per DJDT recommendations, make sure jquery loads before requireJS
<script type="text/javascript" src="/static/websec/external/jquery-2.1.1.min"></script>



<script type="text/javascript" src="{{STATIC_URL}}websec/external/require.js"></script>
<script>
//defines the requirejs configuration in general.
{% include "websec/requirejs_config.html" %}
</script>
Community
  • 1
  • 1
JL Peyret
  • 10,917
  • 2
  • 54
  • 73

2 Answers2

3

It's generally considered bad practice to use the built-in admin backend for end-users.

Try checking foy <body></body> tags in the pages. Without these it will not load.

Then try adding INTERNAL_IPS = ('127.0.0.1') in settings.py

To make it ONLY load for users inside the admin panel, you could add a tag to your custom admin pages and change settings.DEBUG_TOOLBAR_CONFIG['INSERT_BEFORE']
Docs: here

Last to force it to show everywhere, you can try adding this to the settings file:

def show_toolbar(request):
    return True
DEBUG_TOOLBAR_CONFIG = {
    "SHOW_TOOLBAR_CALLBACK" : show_toolbar,
}

to stop debug toolbar from checking if it should appear or not, it always will Only use for testing/development purposes. All other users can see it as well.

Documentation: here.

ss7
  • 2,902
  • 7
  • 41
  • 90
  • Nope, no effect. Restarted django and DEBUG is True, but no change. – JL Peyret Jun 03 '16 at 18:00
  • Yes, I know this is not a normal practice. But the context is that it a VM-based app, so the user on-site has to be able to configure at least some of it via the admin. And it would be nice if the look & feel wasn't totally different in that case. Anyway, really just having a look at it, I may decide to stick with accepted wisdom and write custom pages. – JL Peyret Jun 03 '16 at 18:02
  • 1
    Try INSERT_BEFORE setting with a custom tag only on the admin pages – ss7 Jun 03 '16 at 18:03
  • tags are there in admin pages. INTERNAL_IPS = ('127.0.0.1') had no effect either. I did not try INSERT_BEFORE. I'll hold off on Django Debug Toolbar, in admin, for now. The **site-packages/django/contrib/admin/templates/admin** directory doesn't have that much stuff in it and it seems fairly self-explanatory so I may not need the Toolbar to find my way after all. – JL Peyret Jun 03 '16 at 18:09
  • You can actually get a package to change the look of the admin to make it nicer. I like suit personally. And I believe you can customize what appears on the first login screen extensively with admin.py – ss7 Jun 03 '16 at 18:10
  • Also, I have turned Django Toolbar off so many times because of requireJS problems that I don't want to spend too much time trying to make it work in this context. But thanks for all your help. – JL Peyret Jun 03 '16 at 18:13
  • No problem, INSERT_BEFORE would normally work to put it on custom admin pages like that only. – ss7 Jun 03 '16 at 18:14
0

A quick overview over what I had to do to get debug_toolbar and admin working together.

You want to load jquery on its own, even though requireJS is configured to handle it as well.

From https://github.com/django-debug-toolbar/django-debug-toolbar/issues/605 :

settings.py

DEBUG_TOOLBAR_CONFIG = {
"JQUERY_URL": None,

}

Now, set up the admin to load jquery outside of requireJS, so debug toolbar can find it:

I was planning to customize that admin anyway, so no big deal creating a base_site.html override.

/myproj/templates/admin/base_site.html

{% block extrahead %}
***************         add this (with proper path)   ************************
<script type="text/javascript" src="{{STATIC_URL}}websec/external/jquery-2.1.1.min.js"></script>
*******************************************************************
<script type="text/javascript" src="{{STATIC_URL}}websec/external/require.js"></script>
<script>
{% include "websec/requirejs_config.html" %}
require(['bootstrap'], function(bootstrap){
});
</script>
{% endblock %}

This is similar to what I had to do to my site's ancestor template to get debug_toolbar and requireJS to cohabit:

/myproj/templates/websec/__base.html

{% block requirejs %}

<script type="text/javascript" src="/static/websec/external/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="{{STATIC_URL}}websec/external/require.js"></script>
<script>
//defines the requirejs configuration in general.
{% include "websec/requirejs_config.html" %}
</script>
{% endblock requirejs %}

Unrelated, but a nice hack to get rid of debug_toolbar during unit testing:

settings.py

INSTALLED_APPS = (
    'django.contrib.auth',
    #... more stuff


    'debug_toolbar',

    'myapp',
    'djcelery',
    'crispy_forms',
)

#hack, but debug toolbar totally collapses performance on some tests
pgm = os.path.basename(sys.argv[0])
if pgm.startswith("test") or pgm.startswith("nosetests"):

    li = [app for app in INSTALLED_APPS if not app == "debug_toolbar"]
    INSTALLED_APPS = tuple(li)
JL Peyret
  • 10,917
  • 2
  • 54
  • 73