0

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

2
  • You should at least set the default quantity to 0 or empty to avoid a bunch of 1s showing up in your quantities list. Commented Nov 24, 2022 at 19:23
  • "Value should be greater than or equal to 1". If I set min=0, then it will be the same as with min=1 Commented Nov 24, 2022 at 21:02

1 Answer 1

1
<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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is ok, but if I select a checkbox with id=1, and the quantity is 0, and the checkbox with id=2 is not checked, and the quantity is >= 1, then it will work incorrect
Had a rethink and tried another approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.