I have a page with a list of items which includes their id, name, price and stock level. This information is gotten from a SQLite3 database and entered onto the page in a table. The table also has a column where the user can add or remove stock of the item.
This code shows a section of the table:
% for i in items:
<tr>
<th scope="row">{{i[0]}}</th>
<td>{{i[1]}}</td>
<td>{{i[2]}}</td>
<td>{{i[3]}}</td>
<td><form method="post" action="/worker"><input type="number" name="newStock.{{i[0]}}"><input style="margin-left: 5px" type="submit" Value="Submit"></form></td>
</tr>
% end
And here is the Python bottle code for this page
@route('/worker', method='GET')
def workerPage1():
return template('worker', items=store.printItems(2)) # 1 prints out for shoppers, 2 prints out for workers.
@route('/worker', method='POST')
def workerPage2():
# allows to receive ItemId
for k in request.forms:
if k.startswith('newStock.'):
itemId = k.partition('.')[-1]
numStock = request.forms.get(k)
store.updateStock(itemId,numStock) # this accesses the database, updating the stock level
return template('worker', items=store.printItems(2))
The problem I am getting, is that when I enter the stock to be added, say for example '8' it does it fine. But then when I refresh the page, it adds another 8 onto it. So if it was at 22 and then I click submit to add the stock, it would go to 30 and then when I refreshed the page it would go to 38, etc.
Any idea on how to stop this from happening?
Thanks.
Instead of
return template('worker', items=store.printItems(2))
inworkerPage2()
, I usedreturn redirect('worker')
. Works as I wanted.