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

时间:2020-05-08 06:36:14

标签: python django

我遇到了代码问题,更具体地讲是一个错误,那就是当我第一次将商品添加到购物车中时(以绿色和中号为例),它被很好地添加了。然后,当我添加另一个项目(假设是“蓝色”和“小”)时,它也可以工作。但是,当我从order_summary.html中增加商品数量时,它是在增加我未点击的其他商品的数量(如果我单击了红色和中号,蓝色和大型,则数量增加了)并说:指定所需的变体。为什么会这样呢?还值得注意的是,当我从单个产品页面添加具有相同变体的相同商品时,它工作正常。我认为此错误的发生是由于我的观点的撰写方式。我试图自己解决它,但我迷路了。有人可以帮我吗?预先感谢!

我的模型。py:

class Item(models.Model):
   title = models.CharField(max_length=120)
   price = models.FloatField()

class Variation(models.Model):
   item = models.ForeignKey(Item, on_delete=models.CASCADE)
   name = models.CharField(max_length=50) # size, color

class ItemVariation(models.Model):
   variation  = models.ForeignKey(Variation, on_delete=models.CASCADE)
   value = models.CharField(max_length=50) # small, medium large etc

class OrderItem(models.Model):
   user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
   item  = models.ForeignKey(Item, on_delete=models.CASCADE)
   item_variations = models.ManyToManyField(ItemVariation)
   quantity = models.IntegerField(default=1)
   ordered = models.BooleanField(default=False)

class Order(models.Model):
   user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
   ref_code = models.CharField(max_length=20)
   ordered = models.BooleanField(default=False)
   items = models.ManyToManyField(OrderItem)
   start_date = models.DateTimeField(auto_now_add= True)
   ordered_date = models.DateTimeField()

我的views.py:

@login_required
def add_to_cart(request, slug):
    item = get_object_or_404(Item, slug=slug)
    variations = request.POST.getlist('variations', [])
    print(variations)
    print(request.POST)
    minimum_variation_count = Variation.objects.filter(item=item).count()
    print(minimum_variation_count)
    print(len(variations))
    if len(variations) < minimum_variation_count:
        messages.info(request, "Please specify the required variations.")


    order_item_qs = OrderItem.objects.filter(
        item=item,
        user= request.user,
        ordered=False,
    )

    for v in variations:
        print(v)
        order_item_qs = order_item_qs.filter(
            item_variations__value=v
        )

    if order_item_qs.exists():
        order_item = order_item_qs.first()
        order_item.quantity += 1
        # messages.success(request, "Product quantity was updated.")
        order_item.save()
    else:
        order_item = OrderItem.objects.create(
            item=item,
            user= request.user,
            ordered=False,
        )

        # order_item.item_variations.add(*variations)
        item_variations_to_add = ItemVariation.objects.filter(
                variation__item=item,
                value__in=variations
            ).values_list('id', flat=True)
        order_item.item_variations.add(*item_variations_to_add)
        order_item.save()

    order_qs = Order.objects.filter(user=request.user, ordered=False)
    if order_qs.exists():
        order = order_qs[0]
        if not order.items.filter(item__id=order_item.id).exists():
            order.items.add(order_item)
            # print(request.POST.getlist('variations', None)) 
            messages.success(request, "Product added to cart.")
            return redirect("order-summary")
    else:
        ordered_date = timezone.now()
        order = Order.objects.create(user=request.user, ordered_date=ordered_date)
        order.items.add(order_item)
        # print(request.POST.getlist('variations', None)) 
        messages.success(request, "Product added to cart.")
        return redirect("order-summary")
    return redirect("order-summary")


@login_required
def remove_single_item_from_cart(request, slug):
    item = get_object_or_404(Item, slug=slug)
    variations = request.POST.getlist('variations', [])
    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__slug=item.slug).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()
                messages.info(request, "Product quantity was updated.")
            else:
                order.items.remove(order_item)
                order_item.delete()
                messages.info(request, "Product was removed from cart.")
            return redirect("order-summary")
        else:
            messages.info(request, "Product was not in your cart")
            return redirect("product", slug=slug)
    else:
        messages.info(request, "You do not have an active order")
        return redirect("product", slug=slug)
    return redirect("order-summary")

我的order_summary.html(我可以在其中增加数量):


<th scope="row" class="border-0">
  <div class="p-2">
    <img src="{{ order_item.item.image_url }}" alt="" width="70" class="img-fluid rounded shadow-sm">
      <div class="ml-3 d-inline-block align-middle">
        <h5 class="mb-1"> <a href="{{ order_item.item.get_absolute_url }}" class="text-dark
        -inline-block align-middle">{{ order_item.item.title }}</a>
        {% for var in order_item.item_variations.all %}
          <!-- <h6>{{ order_item.item_variations.all }}</h6> -->
          <!-- <ul> -->
            <!-- <li><h6>{{ var.variation.name }}: {{ var.value }}</h6></li> -->
          <!-- </ul> -->
          <h6>{{ var.variation.name }}: {{ var.value }}</h6>
        {% endfor %}
      </div>
  </div>
</th>
{% if order_item.item.discount_price %}
  <td class="border-0 align-middle"><strong>${{ order_item.get_total_discount_item_price }} <span class="badge badge-warning">Saving ${{ order_item.get_amount_saved }}</span></strong></td>
{% else %}
  <td class="border-0 align-middle"><strong>${{ order_item.get_total_item_price }}</strong></td>
{% endif %}
    <td class="border-0 align-middle"><strong>
      <div class="pull-center">
          <a href="{% url 'remove-single-item-from-cart' order_item.item.slug %}" class="btn mr-2"><i class="fa fa-minus"></i></a>
          {{ order_item.quantity }}<a href="{% url 'add-to-cart' order_item.item.slug %}" class="btn ml-2"><i class="fa fa-plus"></i></a>
        </div>      
      </div>
      </strong></td>
    <td class="border-0 align-middle"><a href="{% url 'remove-from-cart' order_item.item.slug %}" class="text-dark"><i class="fa fa-trash"></i></a></td>
  </tr>

我的single_product.html(我可以从这里增加产品数量,并且可以正常工作):

<form class="form" method="POST" action="{{ object.get_add_to_cart_url }}">
                    {% csrf_token %}
                      {% for var in object.variation_set.all %}
                        <h5>Choose {{ var.name }}</h5>
                        <select class="form-control mb-4 col-md-4" name="variations">
                          {% for item in var.itemvariation_set.all %}
                            <option value="{{ item.value }}">{{ item.value|capfirst }}</option>
                          {% endfor %}
                        </select> 
                      {% endfor %}


                    <div class="action">
                      <button class="btn btn-success">Add to Cart</button>
                      <button class="like btn btn-danger" type="button"><span class="fa fa-heart"></span></button>
                    </div>
                  </form>

我的管理员:

from django.contrib import admin
from .models import Item, Variation, ItemVariation


class ItemVariationAdmin(admin.ModelAdmin):
    list_display = ['variation',
                    'value']

    list_filter = ['variation', 'variation__item']
    search_fields = ['value']


class ItemVariationInLineAdmin(admin.TabularInline):
    model = ItemVariation
    extra = 1


class VariationAdmin(admin.ModelAdmin):
    list_display = ['item',
                    'name']
    list_filter = ['item']
    search_fields = ['name']
    inlines = [ItemVariationInLineAdmin]

admin.site.register(Item)
admin.site.register(ItemVariation, ItemVariationAdmin)
admin.site.register(Variation, VariationAdmin)

我的urls.py:

path('add_to_cart/<slug>/', orders_views.add_to_cart, name='add-to-cart'),
path('remove_from_cart/<slug>/', orders_views.remove_from_cart, name='remove-from-cart'),

5 个答案:

答案 0 :(得分:3)

  1. 我希望variations为空,因为您不是通过POST请求发送的,也不是从html表单发送的任何变体输入。这就是为什么您收到"Please specify the required variations."消息的原因。 (len(variations) == 0
  2. 由于variations为空,for v in variations没有执行任何过滤工作。因此,当您到达order_item_qs.first()时,并不一定要获得您期望的OrderItem,而可能是+=1在错误的OrderItem上。

要解决:

  1. 最简单的方法是添加一个increase_quantity url端点,您可以在其中将OrderItem id传递到视图中。然后,您确切地知道要增加数量的OrderItem,而无需进行任何过滤即可找到它。
  2. 否则,您需要弄清楚如何将变体发送到视图。如果使用POST,则您要使用某种输入。如果是GET,则可以附加URL或类似的内容。 (我强烈建议您以POST请求的形式发送。)

答案 1 :(得分:2)

如果您不想更改视图,则order_summary.html需要为每个OrderItem提供一个表单,以在更改数量时过帐相关的变化。

order_summary.html底部附近,将其替换:

<div class="pull-center">
    <a href="{% url 'remove-single-item-from-cart' order_item.item.slug %}" class="btn mr-2"><i class="fa fa-minus"></i></a>
    {{ order_item.quantity }}<a href="{% url 'add-to-cart' order_item.item.slug %}" class="btn ml-2"><i class="fa fa-plus"></i></a>
    </div>
</div>

类似这样:

<div class="pull-center">
    <form method="POST" action="{% url 'add-to-cart' order_item.item.slug %}">
        {% csrf_token %}
        <a href="{% url 'remove-single-item-from-cart' order_item.item.slug %}" 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>
        {% for iv in order_item.item_variations.all %}
        <input type="hidden" name="variations" value="{{ iv.value }}">
        {% endfor %}
    </form>
</div>

您需要确保模板的这一部分尚未包装在表单中。

答案 2 :(得分:2)

您在注释中提到,现在需要解决方案来增加和减少OrderItem数量。现在该采取另一种方法了。

此解决方案提供了一个视图,该视图负责更改已订购项目的数量(正负)。它使用OrderItem主键,它比Item.pk和变体的组合要可靠得多。

将此添加到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=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")

将此添加到您的urls.py

path('update-qty', orders_views.update_qty, name='update-qty'),

order_summary.html底部附近,将其替换:

<div class="pull-center">
    <a href="{% url 'remove-single-item-from-cart' order_item.item.slug %}" class="btn mr-2"><i class="fa fa-minus"></i></a>
    {{ order_item.quantity }}<a href="{% url 'add-to-cart' order_item.item.slug %}" class="btn ml-2"><i class="fa fa-plus"></i></a>
    </div>
</div>

类似这样:

<div class="pull-center">
    <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>
</div>

答案 3 :(得分:1)

我认为您的问题在视图中

@login_required
def add_to_cart(request, slug):
    item = get_object_or_404(Item, slug=slug)

    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("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("order-summary")

我会选择物料管理器来代替物料变化

class VariationManager(models.Manager):
    def all(self):
        return super(VariationManager, self).filter(active=True)

    def sizes(self):
        return self.all().filter(category='size')

    def colors(self):
        return self.all().filter(category='color')


VAR_CATEGORIES = (
    ('size', 'size',),
    ('color', 'color',),
)


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

    objects = VariationManager()
    active = models.BooleanField(default=True)

    def __str__(self):
        return self.title

用于模板

                {% if order_item.variation.all %}
                {% for variation in order_item.variation.all %}
                {{ variation.title|capfirst }}
                {% endfor %}
                {% endif %}

这应该有效

答案 4 :(得分:0)

我宁愿建议在任何应用程序代码中对CRUD视图使用基于类的视图(通用)/基于类的香草视图。对于简单的基于CRUD的视图,基于功能的视图太多了。