Debugging DEBUG behaviour in django admin and DAL - how are you supposed to do it properly?

hi folls. I wonder if another soul might be able to help here.

I’m trying to debug a a frustrating issue in a django admin, with django-autocomplete-light. I’ve linked below to it, and the short version is that when running in DEBUG=True, I’m seeing django admin act as intended with a django multi select being replaces by a natty select2 multi-tag thing.

So it turns thing here:

Into this thing here, which is broadly speaking a nicer user experience, allowing users to add tags on the fly, and choose from a autocompleting list.

This is powered by django-autocomplete-light, and a nifty taggit library.

Here’s the abridged form code


from dal_select2_taggit import widgets as dal_widgets


class HostingAdminForm(forms.ModelForm):


   class Meta:
        model = ac_models.Hostingprovider
        fields = "__all__"
        widgets = {
            "services": LabelWidget(model=ac_models.Service),
            "staff_labels": dal_widgets.TaggitSelect2("label-autocomplete"),
        }

The problem

However, this only seems to work when DEBUG=True is set. When it’s set to false, I’m seeing the same error as is listed here in issue 1195 on the django-autocomplete-light repo.

But it’s really hard for me to figure out what is really happening.

I’ve figured out through trial and error, that minified versions of some js libraries are served when DEBUG is set to False, but I’m because django admin has SO many firm widgets loaded, it’s not obvious to me where to start to see what might be causing this clash.

I’ve also tried “cheating” by looking at the files generated in media section, to see if I can hard code them when DEBUG=True is off. So instead of this:

{% block extrahead %}{{ block.super }}
<script src="{% url 'admin:jsi18n' %}"></script>
{{ media }}
{% endblock %}

I’d have this.



{% block extrahead %}{{ block.super }}
<script src="{% url 'admin:jsi18n' %}"></script>
{{ media }}

<link href="/static/taggit_labels/css/taggit_labels.css" media="all" rel="stylesheet">
<link href="/static/admin/css/vendor/select2/select2.css" media="screen" rel="stylesheet">
<link href="/static/admin/css/autocomplete.css" media="screen" rel="stylesheet">
<link href="/static/autocomplete_light/select2.css" media="screen" rel="stylesheet">
<script src="/static/admin/js/vendor/jquery/jquery.js"></script>
<script src="/static/admin/js/vendor/select2/select2.full.js"></script>
<script src="/static/admin/js/calendar.js"></script>
<script src="/static/admin/js/jquery.init.js"></script>
<script src="/static/autocomplete_light/autocomplete_light.js"></script>
<script src="/static/admin/js/admin/DateTimeShortcuts.js"></script>
<script src="/static/admin/js/core.js"></script>
<script src="/static/admin/js/inlines.js"></script>
<script src="/static/greencheck/js/email.js"></script>
<script src="/static/autocomplete_light/select2.js"></script>
<script src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script src="/static/taggit_labels/js/taggit_labels.js"></script>
<script src="/static/autocomplete_light/i18n/en.js"></script>
<script src="/static/admin/js/actions.js"></script>
<script src="/static/admin/js/urlify.js"></script>
<script src="/static/admin/js/prepopulate.js"></script>
<script src="/static/admin/js/vendor/xregexp/xregexp.js"></script>

{% endblock %}

For what it’s worth it looks like the issue is related to code further down in /static/autocomplete_light/select2.js. And seems to be something related to jQuery not being defined:

Here’s the culprit:

$(document).on('autocompleteLightInitialize', '[data-autocomplete-light-function=select2]', function() {
  // (snip) - do stuff
}

What I don’t understand though, is why this works in DEBUG=True, but not when DEBUG=False. the same files are being loaded.

How DO you debug gnarly js loading issues in the admin like this?

Apart from me grepping the entire source for settings.DEBUG, have you come across any documentation about what DEBUG=True does when serving files in django admin? I don’t think this down to static files being served differently in this case.

I was able to cheat last time, and just keep django-autocomplete-light at version 3.5.1 when I was running an older versino of django, but since upgrading everything else, I can’t put off grasping this nettle anymore.

For the terminally curious

FWIW, I’m working on an open source application where I work at the Green Web Foundation, so while I might be bit embarassed about the state of the code, all of this should be visible for any folks who are really, really good at working with django and autocomplete light libraries in admin like this.

Thanks in advance - I’ve spent hours on this now, so asking in the forum is pretty much one of my last ditch attempts to fix this now…

If anyone is curious and struggling with a similar problem, here’s how I fixed this specific issue - I ended up overriding the default django templates, that were identified by django debug toolbar, and created a custom js file containing jquery and select2, some django autocomplete light js files, all concatenated together.

This was the only thing I could find that worked in both development AND production, after I tested with both gunicorn and django’s default internal server:

I’m aware that it’s not great practice to override admin django templates, and replace a set of js files included via the django media framework with some hardcoded values to solve issues like this.

But this is a year’s old bug in a third party library 2021, and there doesn’t seem to be a clear resolution.

Using tricks like defer to delay loading didn’t seem to help either, so globbing a few js files together to ensure the loading order didn’t seem too heinous.

DAL version 4 looks like it will have some new widgets be based on webcomponents. From what I can tell seem to solve many of the issues I’ve experienced.