Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Django server not displaying the database information in the html template

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!