Sergey
Sergey

Reputation: 137

Django: html input type=input and checkbox

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>&nbsp;</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

Upvotes: 0

Views: 152

Answers (1)

SamSparx
SamSparx

Reputation: 5257

<input class="input" min="1" value=1 type="number" name="quantity">

This tag basically ensures the value will be at least 1. Try

   <input class="input" min="0" value=0 type="number" name="quantity">

For the next part you need to match the correct item with its quantity and skip if the quantity is 0 - but we also need some error checking, and to do that which have to know which quanity goes with which ID. You can do that with something like

<input class="input" min="0" value=0 type="number" name="quantity-{{item.id}}">


def create_order(request):
    #get fields from the form
    items = request.POST.getlist('item')
    #create a result array to hold pairs
    result = []
    for item in items:
        quantity_name = "quantity-" + str(item)
        item_quantity = request.POST.get(quantity_name)
        if item_quantity==0:
            #handle error submitted item with 0 quantity
        else:
            result.append( (item, item_quantity) )   

    return JsonResponse({
        'result':result,
    })

While this won't check for unsubmitted items with positive quantities (you may need JS for that) it will behave as you'd expect a form to.

Upvotes: 1

Related Questions