Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda