codingjoe / django-select2

This is a Django integration for Select2
https://django-select2.rtfd.io
MIT License
172 stars 53 forks source link

i18n is broken due to missing jQuery import in media 🐛 #292

Closed mardukbp closed 3 months ago

mardukbp commented 4 months ago

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

codingjoe commented 3 months ago

@mardukbp can you check how Django handles this for the autocomplete field in admin integration? Otherwise, please don't hesitate to provide a patch. Cheers! Joe

mardukbp commented 3 months ago

I learned how to fix this in the source code of AutocompleteMixin :)

Patch submitted :)

codingjoe commented 3 months ago

🤙 nice 🤙