KeyError в Safari, но работает в Firefox
Пожалуйста, помогите решить проблему!
Я пишу интернет-магазин на Django, возникла необходимость разделить его на розничный/оптовый с сохранением одной базы. В какой-то момент safari начал выдавать мне KeyError, но в Firefox все продолжает работать согласно логике. В чем может быть причина?
Ошибка выглядит следующим образом:
"KeyError at /cart/retail/
'price'
Request Method: GET
Request URL: http://127.0.0.1:8000/cart/retail/
Django Version: 3.2.7
Exception Type: KeyError
Exception Value:
'price'
Exception Location: /Users/vitbashy/Desktop/Projects/dekor-keramika/dekor_keramika/cart/**cart.py**, line 69, in __iter__
Python Executable: /Users/vitbashy/Desktop/Projects/dekor-keramika/venv/bin/python
Python Version: 3.9.6"
cart.py:
def add_retail(self, product, quantity=1, update_quantity=False):
# Add a product to cart or update its quantity
product_id = str(product.id)
if product_id not in self.cart:
self.cart[product_id] = {
'quantity': 0,
'price': str(product.price_retail)
}
if update_quantity:
self.cart[product_id]['quantity'] = quantity
else:
self.cart[product_id]['quantity'] += quantity
self.save()
def save(self):
# Updating the cart session
self.session[settings.CART_SESSION_ID] = self.cart
# Mark the session as "modified" to make sure it is saved
self.session.modified = True
def remove(self, product):
# Removing an item from the cart
product_id = str(product.id)
if product_id in self.cart:
del self.cart[product_id]
self.save()
def __iter__(self):
"""
Search for items in the shopping cart and retrieve products from the database.
"""
product_ids = self.cart.keys()
# Receive product items and add them to cart
products = Product.objects.filter(id__in=product_ids)
for product in products:
self.cart[str(product.id)]['product'] = product
for item in self.cart.values():
item['price'] = Decimal(item['price'])
item['total_price'] = item['price'] * item['quantity']
yield item
def __len__(self):
# Counting all items in the cart
return sum(item['quantity'] for item in self.cart.values())
views.py:
@require_POST
def cart_add_retail(request, slug):
cart = Cartt(request)
product = get_object_or_404(Product, slug=slug)
form = CarttAddProductRetailForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cartt.add_retail(
product=product,
quantity=cd['quantity'],
update_quantity=cd['update']
)
return redirect('cart_detail_retail')
def cart_remove_retail(request, slug):
cart = Cartt(request)
product = get_object_or_404(Product, slug=slug)
cart.remove(product)
return redirect('cart_detail_retail')
def cart_detail_retail(request):
cart = Cart(request)
return render(request, 'cart/detail_retail.html', {'cart': cart})
Я не понимаю, в чем может быть причина этой проблемы.
Заранее спасибо!