Reputation: 145
I recently tried using select2
multi-select box in my django
app, the select2
box displayed properly and I can select multiple options like it should, but there is just a problem when the selected values taken into the array,
This is my code :
html page:
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/css/select2.min.css" rel="stylesheet" />
<script src="{% static 'js/vendor/jquery-3.4.1.min.js' %}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js"></script>
<script>
$(document).ready(function() {
$('.recipientMultiSelect').select2({
placeholder: "Choose Recipient Numbers / Groups",
tags: true
});
});
</script>
</head>
<body>
<form action="{% url 'broadcast_single' %}" method="POST">
{% csrf_token %}
<div class="col-md-4">
<div class="form-group">
<label for="broadcast_name" class="col-form-label"> Broadcast Name: </label>
<input type="text" name="broadcast_name" class="form-control" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="recipients[]" class="col-form-label"> Recipient List: </label>
<select class="recipientMultiSelect form-control" name="recipients[]" multiple="multiple" required>
<option value="aaaaaa">aaaaaa</option>
<option value="bbbbbb">bbbbbb</option>
<option value="cccccc">cccccc</option>
</select>
</div>
</div>
</br>
</br>
</br>
</br>
<div class="col-xs-8 col-md-8">
<div class="form-group">
<label for="broadcast-content" class="col-form-label"> SMS Content: </label>
<textarea name="broadcast_content" rows="15" cols="85" maxlength="160" class="form-control"> </textarea>
</div>
</div>
<div class="col-xs-8 col-md-8">
<div class="form-group">
<button class="btn btn-primary btn-block mt-4" type="submit">Send Broadcast</button>
</div>
</div>
</form>
</body>
Lets assume that all 3 options are selected and passed into the array recipients[]
views.py:
def broadcastSingle(request):
if request.method == 'POST':
bcName = request.POST['broadcast_name']
bcRecArray = request.POST['recipients[]']
bcContent = request.POST['broadcast_content']
print('PRINT : ',len(bcRecArray))
print(', '.join(bcRecArray))
bcRecArray should contain [ 'aaaaaa', 'bbbbbb', 'cccccc'] and the length should be 3
but instead, bcRecArray has [ 'c', 'c', 'c', 'c', 'c', 'c'] and length of 6
I think, the select2
multi-select box only took the last selected value and turn it into an array of the letters in the selected value.
Upvotes: 0
Views: 1462
Reputation: 145
I've found the answer, from this thread : Django: using <select multiple> and POST
I should be using request.POST.getlist('recipients') instead, since the [] wouldn't work in django as its only a convention limited to PHP.
Thank you and I'll have this thread closed.
Upvotes: 1
Reputation: 723
I had the same problem when using bootstrapDualListbox. The solution I came up with was to use a hidden text field, and then on submit, copy the values from the multi-select into the hidden text field:
// "employees" is a jQuery handle to the multi-select.
let data = employees.val();
if (data.length === 0) {
submitError('<p>{{ _('No employees selected') }}</p>', event);
} else {
//
// We want ints, not strings.
//
data = data.map(x => parseInt(x));
snapshot.val(JSON.stringify({"employees": data}));
}
Back on the server, of course the JSON has to be unpacked from the hidden text field as needed.
Upvotes: 0