Я пытаюсь перейти с Slug на Id, связанный с элементом, чтобы исправить ошибку, связанную с количеством в моем проекте.
Итак, я переключил все с slug на id, но продолжаю получать Page not found (404) No Item matches the given query.
Я обновил функции моделей, представление, URL-адреса и шаблон, связанные со сводкой заказа, я не уверен, что мне нужно обновить что-то еще
Вот мои модели, которые я начал с:
class Item(models.Model):
title = models.CharField(max_length=100)
def get_add_to_cart_url(self):
return reverse("core:add-to-cart", kwargs={
'id': self.id
})
def get_remove_from_cart_url(self):
return reverse("core:remove-from-cart", kwargs={
'id': self.id
})
Вот мои URL:
app_name = 'core'
urlpatterns = [
path('', HomeView.as_view(), name='home'),
path('product/<slug>/', ItemDetailView.as_view(), name='product'),
path('add-to-cart/<id>/', add_to_cart, name='add-to-cart'),
path('remove-item-from-cart/<id>/', remove_single_item_from_cart,
name='remove-single-item-from-cart'),
path('remove-from-cart/<id>/', remove_from_cart, name='remove-from-cart'),
Вот представления:
@login_required
def add_to_cart(request, id):
item = get_object_or_404(Item, id=id)
order_item_qs = OrderItem.objects.filter(
item=item,
user=request.user,
ordered=False
)
item_var = [] # item variation
if request.method == 'POST':
for items in request.POST:
key = items
val = request.POST[key]
try:
v = Variation.objects.get(
item=item,
category__iexact=key,
title__iexact=val
)
item_var.append(v)
except:
pass
if len(item_var) > 0:
for items in item_var:
order_item_qs = order_item_qs.filter(
variation__exact=items,
)
if order_item_qs.exists():
order_item = order_item_qs.first()
order_item.quantity += 1
order_item.save()
else:
order_item = OrderItem.objects.create(
item=item,
user=request.user,
ordered=False
)
order_item.variation.add(*item_var)
order_item.save()
order_qs = Order.objects.filter(user=request.user, ordered=False)
if order_qs.exists():
order = order_qs[0]
# check if the order item is in the order
if not order.items.filter(item__id=order_item.id).exists():
order.items.add(order_item)
messages.info(request, "This item quantity was updated.")
return redirect("core:order-summary")
else:
ordered_date = timezone.now()
order = Order.objects.create(
user=request.user, ordered_date=ordered_date)
order.items.add(order_item)
messages.info(request, "This item was added to cart.")
return redirect("core:order-summary")
@login_required
def remove_from_cart(request, id):
item = get_object_or_404(Item, id=id)
order_qs = Order.objects.filter(
user=request.user,
ordered=False
)
if order_qs.exists():
order = order_qs[0]
# check if the order item is in the order
if order.items.filter(item__id=item.id).exists():
order_item = OrderItem.objects.filter(
item=item,
user=request.user,
ordered=False
)[0]
order.items.remove(order_item)
order_item.delete()
messages.info(request, "This item was removed from your cart")
return redirect("core:order-summary")
else:
messages.info(request, "This item was not in your cart")
return redirect("core:product", id=id)
else:
messages.info(request, "You don't have an active order")
return redirect("core:product", id=id)
@login_required
def remove_single_item_from_cart(request, id):
item = get_object_or_404(Item, id=id)
order_qs = Order.objects.filter(
user=request.user,
ordered=False
)
if order_qs.exists():
order = order_qs[0]
# check if the order item is in the order
if order.items.filter(item__id=item.id).exists():
order_item = OrderItem.objects.filter(
item=item,
user=request.user,
ordered=False
)[0]
if order_item.quantity > 1:
order_item.quantity -= 1
order_item.save()
else:
order.items.remove(order_item)
messages.info(request, "This item quantity was updated")
return redirect("core:order-summary")
else:
messages.info(request, "This item was not in your cart")
return redirect("core:product", id=id)
else:
messages.info(request, "You do not have an active order")
return redirect("core:product", id=id)
Вот шаблон
<div class="pull-center">
<form method="POST" action="{% url 'add-to-cart' order_item.id %}">
{% csrf_token %}
<a href="{% url 'remove-single-item-from-cart' order_item.id %}" class="btn mr-2"><i class="fa fa-minus"></i></a>
{{ order_item.quantity }}
<button type="submit" class="btn ml-2"><i class="fa fa-plus"></i></button>
</form>
</div>