Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda