M
M
Mischa_111112021-07-26 20:38:28
Django
Mischa_11111, 2021-07-26 20:38:28

How to search a drop down list in Django?

I have a list:
SPIS = (
('Audio', (
('vinyl', 'Vinyl'),
('cd', 'CD'),
)
),
('Video', (
('vhs', 'VHS Tape'),
('dvd', 'DVD'),
)
),
('unknown', 'Unknown'),
('unknown', 'Unknown'),
)
spis = models.CharField(max_length=7, choices= SPIS, verbose_name='full name')

How can I make it so that when I type the first letter, only last names starting with that letter pop up.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-07-27
@Mischa_11111

Mischa_11111 Django is mostly not about the frontend, but about the backend.
Here's what you can do:
1. Install and configure django-widget-tweaks
2. Add select2
to the template 3. Add the following code to the end of the template:

<script>
$(document).ready(function() {
    $('.select2').each(function (idx, elem) {
        $(elem).select2({
            'language': 'ru',
        });
    });
}
</script>

4. Render our form (or rather the field) like this:
{% load widget_tweaks %}
{% render_field form.spis class+="select2" data-minimum-results-for-search="Infinity" %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question