I am trying to get data from a form, the form is reproduced a number of times based on a list. One form for each item. The form consists of a checkbox and a textfield. If the checkbox is checked then I need the accompanying textfield data as well.
I asked a related question here:
Django validation error u"'' value must be a decimal number."
Which was mostly solved however now I have a new issue.
view:
for item in request.POST.getlist('item_list'):
item_id = int(item)
item = Item.objects.get(id=item_id)
item_name = item.name
print item_name
list = List(name = item_name, created_on = now, edited_on = now)
for price in request.POST.getlist('price'):
if not price:
continue
print price
list_item.price = Decimal(price)
list_item.save()
item.delete()
Its not shown above but now = timezone.now()
.
template:
<form action="" method="post">
{% csrf_token %}
{% for item in item_list %}
<input type="checkbox" name="item" value="{{item.id}}">{{item.name}} <input type="text" name="price"><br>
{% endfor %}
<input type="submit" value="Add Items">
</form>
When I submit the form it now runs through both loops twice and the final prices for all items are identical. I determined this by inserting print functions throughout my code and analyzing what is displayed. I guess I understand where the issue is, the question is how to correct it, any help would be greatly appreciated.