1👍
Your problem is that you are redirecting via request.path_info
, which is just goint to redirect you back to your remove_cart
view.
The reason your first error is "Key 'itemsForRemove' not found in "
is because the first attempt worked and your view redirected to itself as GET, which clearly didn’t have the itemsForRemove POST data.
When you changed your code to “foo”, it’s failing on your first POST stage (which is why you properly see "itemsForRemove"
).
Anyways, fix your redirect problem, then add a check to make sure your view is being called via POST.
def RemoveProductFromCart(request):
if not request.method == 'POST':
return http.HttpResponseForbidden()
removeThis = request.POST['itemsForRemove']
listOfItems = request.session['cart']
for i in removeThis:
del listOfItems[int(removeThis) - 1]
return redirect('somewhere_else')
1👍
If the request doesn’t include the value for itemsForRemove
it will raise an exception. Better way to do this is to use removeThis = request.POST.get('itemsForRemove', '')
, it will give a default of ''
and no exception is raised even if value doesn’t exist.