'Dropdown select multiple with HTMX

I am using Django and I am passing some values to views.py via HTMX

So Frist I have a dropdown list with multiple choices

    <select class="custom-select mb-4" name="Wells3" multiple >
            <option selected>Select Well</option>
            {% for well in FieldWells %}
            <option value="{{well.WellID}}">{{well.WellID}}</option>
            {% endfor %}
    </select>

I selected two or three choices with Ctrl then I send the request via a button ,

<button class="btn btn-primary" hx-get="{% url 'Plot_Logs' %}" hx-target="#LogPlots"
hx-include="[name='Wells3']"
>Plot Logs</button>

so in the views.py I receive only the last value of the selection.

Are there any options to add?

Many thanks



Solution 1:[1]

You probably need a request.getlist in your view:

Here's my template fragment - very similar to yours. Note the hx-vals to pass in other parameters (in my case taken out of a global $store):

<select x-ref="select" multiple
        name="select_series"
        hx-include="[name='select_series']"
        hx-get="{% url 'grid_index' %}"
        hx-vals="javascript:{...get_parameters()}"
        hx-target="#grid"
        hx-indicator="#htmx-indicator">
    {% for this_series in series %}
        <option value="{{ this_series.value }}">
            {{ this_series.get_label }}
        </option>
    {% endfor %}
</select>

In my view I have:

select_series = request.GET.getlist('select_series', [])

The unusual thing is request.GET.get appears to work fine (& returns a one value) which is not really helpful.

Hope this helps

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J Harley