14. Displaying product size based on color selected
def view_product(request, product_id, id_color = None) : path('product/<int:product_id>', view_product, name="view_product"),
path('product/<int:product_id>/<int:id_color>', view_product, name="view_product"),def view_product(request, product_id, id_color = None) :
has_stock = False
sizes = {}
colors = {} #? needs to be declaredif len(item_stock) > 0 :
has_stock = True #? necessary in order to do a if on the html. if the product is out of stock, will show "Out of Stock"
colors = {item.color for item in item_stock} #? gets the colors of all products, uses sets '{}' to avoid duplicate colors
if id_color :
item_stock = ItemStock.objects.filter(product = product, quantity__gt = 0, color__id = id_color) #? gets the color id attribute from the Color class (that is automatically created)
sizes = {item.size for item in item_stock} #? gets the sizes of all products

Last updated