I have a problem with send data from input type='number' to django view.
I have a page with products, each of them has a checkbox and a quantity selection (input type='number')
<form action="{% url 'create-order' %}" method="POST">
{% csrf_token %}
<table class="table table-responsive table-borderless">
<thead>
<th> </th>
<th>Quantity</th>
</thead>
<tbody>
{% for item in items %}
<tr class="align-middle alert border-bottom">
<td>
<input type="checkbox" id="check" name="item" value="{{ item.id }}">
</td>
<td>
<input class="input" min="1" value=1 type="number" name="quantity">
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="submitButton">
<button type="submit" class="lgbtn green">Go to order</button>
</div>
</form>
Submit button go to view:
def create_order(request):
quantities = request.POST.getlist('quantity')
items = request.POST.getlist('item')
return JsonResponse({
'quantities': quantities,
'items': items
})
For example, I have 6 products with id = 1, 2, 3, 4, 5, 6. And if I choose 1, 2, 3 and set quantities: 3, 4, 5, then I get:
items = [1, 2, 3] # it's OK
quantities = [3, 4, 5, 1, 1, 1] # but I need [3, 4, 5]
Ideally, I want items and quantities to be in the same object (For example [(1, 3), (2, 4), (3, 5)] or dict {1: 3, 2: 4, 3: 5}), but not necessarily, but in any case, I need to select the quantity only for those items that have been checked
quantitieslist.