Skip to content
Advertisement

How do I display my products by certain category

So I am making an ecom site, and I am using Django. I am quite new and I am using a tutorial but have stepped off touch with it as I want to have 2 categories and maybe more on my website in the future. I want to be able to display them by their category, and I asked for help around my discord server and some people have tried to help and it does go through all my products however I have an if statement and that if statement gets ignored

So can anyone show me a way to display products with their category set as Specials only? Or hatbox only?

Store.Html

{% extends 'store/main.html' %}
{% load static %}
{% block content %}
    <title>Store</title>
    <h2>Our Stylish Hat Boxes - from the smallest to the largest.</h2>
    <div class="row" style="padding-bottom: 5%;">
        
        {% if products.category == Hatbox %}
        {% for product in products %}
        
        <div class="col-sm-4-fluid" style="margin-right: 5%;">
        <div class="card" style="width:300px;">
            <img class="card-img-top" src="{{product.imageURL}}" alt="Card image">
            <div class="card-body">
                <h5 style="display: inline-block; float:right;"><strong>
                £ {{product.price|floatformat:2}}</strong></h5>
                <h6 class="card-title">{{product.name}}</h6>
                <a href="#" class="btn btn-primary">View</a>
                </div>
            </div>
        </div>
        
        {% endfor %}
        {% endif %}
        
        </div>
    <h2>Something Special...</h2>
    <div class="row" style="padding-bottom: 5%;">
        
        {% for product in products %}
        {% if products.categorys.name == "Specials" %}

        <div class="col-sm-4-fluid" style="margin-right: 5%;">
            <div class="card" style="max-width:300px;min-width: 50px;">
                <img class="card-img-top" src="{{product.imageURL}}" alt="Card image">
                <div class="card-body">
                    <h5 style="display: inline-block; float:right;"><strong>
                    £ {{.price|floatformat:2}}</strong></h5>
                    <h6 class="card-title">{{product.name}}</h6>
                    <a href="#" class="btn btn-primary">View</a>
                    </div>
                </div>
            </div>
            
            {% endif %}
            {% endfor %}
            
            
    </div>
        
{% endblock content %}

views.py

from django.shortcuts import render
from .models import *

# Create your views here.
def store(request):
    products = Product.objects.all()
    categorys = Category.objects.all()

    size = Size.objects.all()
    context = {
        "categorys":categorys,
        'products':products,
        'size':size
    }
    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)

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

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

models.py

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

# Create your models here.

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

    def __str__(self):
        return self.name

class Category(models.Model):
    nameOfCategory = models.CharField(max_length=200)

    def __str__(self):
        return self.nameOfCategory

class Size(models.Model):
    size = models.CharField(max_length=200)

    def __str__(self):
        return self.size

class Product(models.Model):
    name = models.CharField(max_length=200, blank=True)
    size = models.ForeignKey(Size, null=True, blank=True, on_delete=models.CASCADE)
    price = models.FloatField()
    ribbon_band = models.CharField(max_length=200, null=True, blank=True)
    flowers = models.CharField(max_length=200,null=True, blank=True)
    exterior_band = models.CharField(max_length=200, null=True, blank=True)
    diamondFlowers = models.BooleanField(default=False)
    category = models.ForeignKey(Category, null=True, blank=True, on_delete=models.CASCADE)
    image = models.ImageField(null=True, blank=True)

    def __str__(self):
        return self.name

    @property
    def imageURL(self):
        try:
            url = self.image.url
        except:
            url = ''
        return url

class Order(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
    date_ordered = models.DateTimeField(auto_now_add=True)
    complete = models.BooleanField(default=False)
    transaction_id = models.CharField(max_length=100, null=True)

    def __str__(self):
        return str(self.id)

class OrderItem(models.Model):
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
    quantity = models.IntegerField(default=0, null=True, blank=True)
    date_added = models.DateTimeField(auto_now_add=True)


class ShippingAddress(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
    address = models.CharField(max_length=200, null=False)
    city = models.CharField(max_length=200, null=False)
    state = models.CharField(max_length=200, null=False)
    zipcode = models.CharField(max_length=200, null=False)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.address

admin.py

from django.contrib import admin

from .models import *

admin.site.register(Customer)
admin.site.register(Product)
admin.site.register(Order)
admin.site.register(OrderItem)
admin.site.register(ShippingAddress)
admin.site.register(Category)
admin.site.register(Size)

Advertisement

Answer

The if statement in the code won’t work as the products inside {% if products.categories == Hatbox %} does not have any categories since its not a single category object. The if statement is also missing quotes around Hatbox. Try this below:

Views

def store(request):
    categorys = Category.objects.all()

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

store.html

    {% for category in categorys %}
        <h1>{{ category.nameOfCategory }}</h1>
        {% for product in category.product_set.all %}
            {{ product.name }}
            {{ product.price }}
            {{ product.size.size }}
        {% endfor %}
    {% endfor %}

Have a read add the docs here and some StackOverflow links here and here for related_names.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement