Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mardukbp opened this issue Jul 23, 2024 · 3 comments · Fixed by #295
Closed

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

mardukbp opened this issue Jul 23, 2024 · 3 comments · Fixed by #295
Assignees
Labels
bug Something isn't working

Comments

@mardukbp
Copy link

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 mardukbp added the bug Something isn't working label Jul 23, 2024
@codingjoe
Copy link
Owner

@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

@codingjoe codingjoe assigned mardukbp and unassigned codingjoe Aug 4, 2024
@mardukbp
Copy link
Author

mardukbp commented Aug 5, 2024

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

Patch submitted :)

@codingjoe
Copy link
Owner

🤙 nice 🤙

mardukbp added a commit to mardukbp/django-select2 that referenced this issue Aug 29, 2024
…oe#295)

Select2Mixin now loads admin/js/jquery.init.js, just like the AutocompleteMixin of Django Admin.

---------

Co-authored-by: Johannes Maron <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants