Home > Mobile >  Django server not displaying the database information in the html template
Django server not displaying the database information in the html template

Time:10-16

I am trying to do an ecommerce and when creating the database it does not update in the checkout.html. page. Below the code.

views.py def cart(request): if request.user.is_authenticated: customer = request.user.customer order, created = Order.objects.get_or_create(customer = customer, complete = False) items = order.orderitem_set.all() else: items = [] order = {'get_cart_total':0, 'get_cart_items':0} context = {'items':items, 'order':order} return render(request, 'store/cart.html', context)

def checkout(request):
if request.user.is_authenticated:
    customer = request.user.customer
    order, created = Order.objects.get_or_create(customer = customer, complete = False)
    items = order.orderitem_set.all()
else:
    items = []
    order = {'get_cart_total':0, 'get_cart_items':0}
context = {}
return render(request, 'store/checkout.html', context)

checkout.html

                {% for item in items %}
                <div class="cart-row">
                    <div style="flex:2"><img class="row-image" src="{{item.product.imageURL}}"></div>
                    <div style="flex:2"><p>{{item.product.name}}</p></div>
                    <div style="flex:1"><p>${{item.product.price|floatformat:2}}</p></div>
                    <div style="flex:1"><p>x{{item.quantity}}</p></div>
                </div>
                
            {% endfor %}

it works for cart.html but it does not work for checkout.html and there is no errors shown to look for. Any help will be appreciated.

CodePudding user response:

The context is empty. The context is a dictionary where the names of the variables used in the template map to the corresponding values.

If you thus want to pass items to the template, you write:

def checkout(request):
    if request.user.is_authenticated:
        customer = request.user.customer
        order, created = Order.objects.get_or_create(customer = customer, complete = False)
        items = order.orderitem_set.all()
    else:
        items = []
        order = {'get_cart_total':0, 'get_cart_items':0}
    # pass  items ↓    pass order ↓
    context = {'items': items, 'order': order }
    return render(request, 'store/checkout.html', context)

Here the items will be an empty queryset, since you take the orderitem_set of the order, but at that point you did not create an OrderItem objects that are linked to that Order, you thus likely should first create OrderItems for the created order.

  • Related