D
D
Dmitry2019-09-07 23:33:13
Django
Dmitry, 2019-09-07 23:33:13

Select2 not working in Django. What to do?

Hello.
Can't apply Select2 autocomplete selector in my form in Django.
It seems that I do everything according to the instructions :
installed django_select2;
added to INSTALLED_APP;
added path('select2/', include('django_select2.urls')),to urls;
added import to forms.py:

from django_select2.forms import Select2Widget, Select2MultipleWidget

and in class Metafor forms.ModelFormadded
widgets = {
            'mat_div_id': Select2Widget,
            'mat_speaker_id': Select2MultipleWidget,
        }

Well, I added {{ form.media.css }}in {% block extrahead %}, {{ form.media.js }}- before {% endblock content %}.
Also, just in case, I installed django-jquery , added it jqueryto INSTALLED_APP and wrote
<script type="text/javascript" src="/static/js/jquery.js"></script>
in the base template.
The only change is that instead of the default "--------" in the mat_div_id field, there is now an empty string (but you can't enter anything into it, just click and get a regular drop-down list).
Maybe you need something else that is not indicated in the documentation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-09-08
@DmRvng

The problem was solved when

<script type="text/javascript" src="/static/js/jquery.js"></script>
moved from the base template to the template with the form, and placed {{ form_m.media.js }} under the line with the script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question