Adding Post request to Template

Submitted 3 years, 10 months ago
Ticket #17
Views 238
Language/Framework Python
Priority Low
Status Closed

Helloo Teckiys, 

I am trying to fix the add to cart and single item remove from the cart and I am trying to fix the template to accommodate the views.py

I am trying to add the code that sends a POST request with form data to the add to cart view and single item remove from the cart but I am not getting the same error which is indicating that the order_item.variation.add(*item_var) does nothing

here is the view.py 


@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("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_single_item_from_cart(request, slug):
    item = get_object_or_404(Item, slug=slug)
    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()
            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", slug=slug)
    else:
        messages.info(request, "You do not have an active order")
        return redirect("core:product", slug=slug)

here is the template which I am failing to add a form request 

        <h2> Order Summary</h2>
        <table class="table">
            <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">Item Title</th>
                <th scope="col">Price</th>
                <th scope="col">Quantity</th>
                <th scope="col">Size</th> 
                <th scope="col">Total Item Price</th>
            </tr>
            </thead>
            <tbody>
            {% for order_item in object.items.all %}
            <tr>
                <th scope="row">{{ forloop.counter }}</th>
                <td>{{ order_item.item.title }}</td>
                <td>{{ order_item.item.price }}</td>
                <td>
                
<----------------------------------The error is in this section---------------------------->
<------------I am trying to add a post request to remove-single-item-from-cart------------->

<a href="{% url 'core:remove-single-item-from-cart' order_item.item.slug %}"><i class="fas fa-minus mr-2"></a></i>
                {{ order_item.quantity }}

<--------------------I am trying to add a post request to add to cart---------------------->

<a href="{% url 'core:add-to-cart' order_item.item.slug %}"><i class="fas fa-plus ml-2"></a></i>
                </td>  

<------------------------------------------------------------------------------------------>
              
                <td>
                {% if order_item.variation.all %}
                {% for variation in order_item.variation.all %}
                {{ variation.title|capfirst }}
                {% endfor %}
                {% endif %}
                </td> 

Submitted on Jun 06, 20
add a comment

1 Answer

Verified

Acknowledged

Submitted 3 years, 10 months ago

so what we understand. removing quantity is not working as expected ? .

- Vengat 3 years, 10 months ago

adding and removing items, if I added an item with a size of small, medium and large and change the quantity in the order summary template, the quantity of the very first item added to the cart is the only item affected no response from the other items with different variants that was actually changed.

- ahmed.hisham87 3 years, 10 months ago

We reviewed the code.. Its kind of functional issue. so we may need to investigate further on this.. We will review the code again . If required, we will setup the call with you..

- Vengat 3 years, 10 months ago

Yes sure let us arrange it

- ahmed.hisham87 3 years, 10 months ago

Can we connect around 5.30 PM PST today?.. Its hard to figure out from the code.

- Vengat 3 years, 10 months ago

yes sure

- ahmed.hisham87 3 years, 10 months ago

sent you the Zoom link. Lets meet at 5.30 PM

- Vengat 3 years, 10 months ago

as discussed, change the a tag paramter and apply the new logic to handle it.

- Vengat 3 years, 10 months ago

Hope you resolved this issue. So we go ahead and close the ticket.

- scott 3 years, 10 months ago


Latest Blogs