Django:将产品添加到购物车无法正常工作

时间:2020-08-02 16:28:37

标签: python django

我正在尝试解决一个与项目数量有关的错误,该错误未能在“订单摘要”中正确反映。因此,我尝试将视图从“添加或删除物料”更改为“合并数量”并将订单物料作为主键,并将其组合为一个功能:

现在我正在更新,我不断得到

This page isn’t working If the problem continues, contact the site owner.
HTTP ERROR 405

当我尝试更改模板时,我收到另一个错误

Reverse for 'update-qty' with keyword arguments '{'slug': 't-shirt-no1fdgfg'}' not found. 1 pattern(s) tried: ['update\\-qty$']

我需要帮助解决此问题:

这是模型。py

class Item(models.Model):
    title = models.CharField(max_length=100)
    keywords = models.CharField(max_length=100)

    def get_update_qty_url(self):
        return reverse("core:update-qty", kwargs={
            'slug': self.slug
        })

class Variation(models.Model):
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    category = models.CharField(
        max_length=120, choices=VAR_CATEGORIES, default='size')
    objects = VariationManager()

class OrderItem(models.Model):
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    quantity = models.IntegerField(default=1)
    variation = models.ManyToManyField(Variation)

class Order(models.Model):
    items = models.ManyToManyField(OrderItem)

这是views.py


@login_required
def update_qty(request):
    if request.method == 'POST':
        item_slug = request.POST.get('item_slug', None)
        # Check for an order_item
        order_item_pk = request.POST.get('order_item', None)
        order_item = OrderItem.objects.filter(pk=order_item_pk).first()
        if not order_item:
            messages.info(request, "Product was not in your cart")
            return redirect("product", slug=item_slug)
        # Check for an active order
        order = Order.objects.filter(user=request.user, ordered=False).first()
        if not order:
            messages.info(request, "You do not have an active order")
            return redirect("product", slug=item_slug)
        # Check that order_item is in active order
        if not order_item.order == order:
            messages.info(request, "Product was not in your cart")
            return redirect("product", slug=item_slug)
        # Update quantities
        action = request.POST.get('action', None)
        if action == "plus":
            order_item.quantity += 1
            order_item.save()
            messages.info(request, "Product quantity was updated.")
        elif action == "minus":
            order_item.quantity -= 1
            if order_item.quantity < 1:
                order_item.delete()
                messages.info(request, "Product was removed from cart.")
            else:
                order_item.save()
                messages.info(request, "Product quantity was updated.")
    return redirect("order-summary")

以下是网址:

app_name = 'core'

urlpatterns = [
    path('', HomeView.as_view(), name='home'),
    path('update-qty', update_qty, name='update-qty'),
]

此处是产品详细信息,您可以在其中选择尺寸变化以转到订单摘要。从这里我得到指示的2个错误

<form method="POST" action="{{ item.get_update_qty_url }}">
{% csrf_token %}
<input class="btn btn-primary btn-md my-2 p" type="submit" value="update_qty">

{% if object.variation_set.all %}
{% if object.variation_set.sizes %}

<select class="form-control" name="size">

{% for items in object.variation_set.sizes %}
<option value="{{ items.title|lower }}">{{ items.title|capfirst }}</option>
{% endfor %}                      

</select>

{% endif %}
{% endif %}

</form>        

这是订单摘要:

<form method="POST" action="{% url 'update-qty' %}">
{% csrf_token %}
<button type="submit" name="action" value="minus" class="btn mr-2"><i class="fa fa-minus"></i></button>
{{ order_item.quantity }}<button type="submit" name="action" value="plus" class="btn ml-2"><i class="fa fa-plus"></i></button>
<input type="hidden" name="item_slug" value="{{ order_item.item.slug }}">
<input type="hidden" name="order_item" value="{{ order_item.pk }}">
</form>

0 个答案:

没有答案