django-select2 icon indicating copy to clipboard operation
django-select2 copied to clipboard

i18n is broken due to missing jQuery import in media 🐛

Open mardukbp opened this issue 1 year ago • 0 comments

Bug Description

When using django-select2 in the Django Admin (regardless of the value of settings.LANGUAGE_CODE) the corresponding i18n file (e.g. en.js) throws the following exception in the brower console:

TypeError: Cannot read properties of undefined (reading 'define')
    at en.js:3:106
    at en.js:3:755

The problem is that jquery.init.js is loaded before this file. In order to load it afterwards this line has to be replaced with:

            js=select2_js + i18n_file + ["admin/js/jquery.init.js"] + ["django_select2/django_select2.js"],

Steps to Reproduce

  1. Create the following model:
from django.db import models

class Person(models.Model):
    title = models.CharField(max_length=10, choices=[('Dr.', 'Dr.'), ('Rep.', 'Rep.')])
  1. Create the following admin for the model:
from django.contrib import admin
from django import forms

from django_select2.forms import Select2Widget

from .models import Person


class PersonForm(forms.ModelForm):
    class Meta:
        widgets = {
            'title': Select2Widget
        }


@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    form = PersonForm
  1. Visit the admin page for the model (admin/app/person/add/)

  2. Open the browser console and find the exception thrown in en.js

Expected Behavior

No exception is thrown and the i18n works as expected

mardukbp avatar Jul 23 '24 10:07 mardukbp