user109503
user109503

Reputation: 41

I cant make my website show its content although i got the same codes from my reference

So ive been learning django and i am practicing to make an ecommerce website from Programming with Mosh. I faced a problem at the part where I cannot display my products at the website. I cannot figure out what I did wrong so I changed reference and used dennis ivy's ecommerce website tutorial. And now, I am facing the same problem. I compared his code with mine and its just the same but I really dont know what im doing wrong.

This is his code: https://i.sstatic.net/Ossxm.png

And this is the result he get: https://i.sstatic.net/On1ri.png (he used for loop and it accessed everything from his products model

This is my code:

{% extends 'store/main.html' %}
{% load static %}
{% block content %}
    <div class="row">
        {% for product in products %}
        <div class="col-lg-4">
            <img class='thumbnail' src="{% static 'images/2+placeholder.png' %}" alt="">
            <div class="box-element product">
                <h6><strong>{{product.name}}</strong></h6>
                <hr>

                <button class="btn btn-outline-secondary add-btn">Add to cart</button>
                <a class='btn btn-outline-success' href="#">View</a>
                <h4 style="display: inline-block; float: right"><strong>{{product.price}}</strong> 
                </h4>
            </div>
        </div>
        {% endfor %}
    </div>
{% endblock content %}

And this is the result i get: https://i.sstatic.net/WsPgT.png

This is my views:

from django.shortcuts import render
from .models import *


def store(request):
    products = Product.objects.all()
    context = {'product': products}
    return render(request, 'store/store.html', context)

def cart(request):
    context = {}
    return render(request, 'store/cart.html', context)

def checkout(request):
    context = {}
    return render(request, 'store/checkout.html', context)

This is my models(up to Product):

from django.db import models
from django.contrib.auth.models import User


class Customer(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, 
            blank=True)
    name = models.CharField(max_length=100, null=True)
    email = models.CharField(max_length=100, null=True)

    def __str__(self):
        return self.name


class Product(models.Model):
    name = models.CharField(max_length=100, null=True)
    price = models.FloatField()

    def __str__(self):
        return self.name

Can someone please enlighten my stupid little tiny brain? Im so drained about this, Im stuck.

Upvotes: 1

Views: 103

Answers (1)

Arjun Shahi
Arjun Shahi

Reputation: 7330

Your context name for products is set for product in your view so change this.

def store(request):
    products = Product.objects.all()
    context = {'products': products}

Upvotes: 2

Related Questions