17. Displaying cart

Next, we will ensure that the ordered items are displayed within the cart page.

Given that new_context.py is used for global displays shown on all pages, we made changes specifically within the cart view function to ensure the ordered items are displayed correctly on the cart page.

Most lines were copied from the cart function in new_context.py, with only minor variations explained below:

views.py
def cart(request): 
    if request.user.is_authenticated:
        client = request.user.client
    
    order, created = Order.objects.get_or_create(client=client, finished=False) 
    items_ordered = OrderedItem.objects.filter(order = order)
    context = {"order" : order, "items_ordered" : items_ordered}
    return render(request, 'cart.html', context) 

At present, the else-statement has been removed as the focus is solely on authenticated users. Additionally, modifications will be made to the main Order class, so it has been included within our context variable instead of using the product_amount_cart variable.

In the cart.html file, we initially displayed the ID of the order. Following that, we will iterate over all the items ordered in that specific order, showcasing various details about them: their image, size, color, name, and quantity.

cart.html
{% extends 'base.html' %}
{% load static %}

{% block body %}

<h3>Cart</h3>
<h4>Order ID: {{ order.id }}</h4>
{% for item in items_ordered %}
<p>
    <img src="{{ item.itemstock.product.image.url }}" width = "60" height = "80">
    Product: {{ item.itemstock.product.name }}; Color: {{ item.itemstock.color.name }}; Size: {{ item.itemstock.size }}; Quantity: {{ item.quantity}}
</p>
{% endfor %}
<a href = "{% url 'checkout' %}">Checkout</a>

{% endblock %}
Note that the products displayed may vary depending on which ones were added via the admin page.

Last updated