Most of our functionality is there, but we still need to make sure that we can delete items from our shopping list. To d this, we will add a new /ID/delete route that accepts POST requests, similar to the edit() view function. This new delete() view function will receive the ID of the post to be deleted from the URL.
10. Deleting items
10.1 Editing the app.py file
In the app.py file:
@app.route(‘/<int:id>/delete’, methods=(‘POST’,))
def delete(id):
post = get_item(id)
conn = get_db_connection()
conn.execute(‘DELETE FROM items WHERE id = ?’, (id,))
conn.commit()
conn.close()
flash(‘”{}” was successfully deleted!’.format(item[‘item’]))
return redirect(url_for(‘index’))
We don’t render the template. Instead we will just edit the first page.
10.2 Editing the edit.html file
In the edit.html file:
<hr>
<form action=”{{ url_for(‘delete’, id=post[‘id’]) }}” method=”POST”>
<input type=”submit” value=”Delete Post”
class=”btn btn-danger btn-sm”
onclick=”return confirm(‘Are you sure you want to delete this post?’)”>
</form>
{% endblock %}