Django issue, Categories Page can't be uploaded with products in it

47 Views Asked by At

$ hello, I tried load category products list by django as shown in codes below, and it never load , and always give error, even the "Int : id" of the category doesn't seen, please I need some help to figure out what is my problem, ... hello, I tried load category products list by django as shown in codes below, and it never load , and always give error, even the "Int : id" of the category doesn't seen, please I need some help to figure out what is my problem, thank you

$ Models.py

class Category(models.Model):
  title = models.CharField(max_length=100)
  image = models.ImageField(upload_to='category_imgs/%Y/%m/%d/')
  class Meta:
    verbose_name_plural = '2. Categories'
  def image_tag(self):
    if self.image:
        return mark_safe('<img src="%s" width="50" height="50" />' % (self.image.url))
    return ""
  def __str__(self):
    return self.title

$Views.py

def category_product_list(request, category_id):
  category = Category.objects.get(pk=category_id)
  data = Product.objects.filter(category=category).order_by('-id')
  categories = Product.objects.distinct().values('category__title', 'category__id')
  brands = Product.objects.distinct().values('brand__title', 'brand__id')
  colors = ProductAttribute.objects.distinct().values('color__title', 'color__id', 
  'color__color_code')
  sizes = ProductAttribute.objects.distinct().values('size__title', 'size__id')
  context = {
    'data': data,
    'categories': categories,
    'brands': brands,
    'colors': colors,
    'sizes': sizes,
    'basic':
        {'main': 'Project',
            'page':'Category Products',
        },
  }
  return render(request, 'main/category_product_list.html', context)

$urls.py

urlpatterns=[
  path('', views.home, name='home'),
  path('category-list', views.category_list, name='category-list'),
  path('brand-list', views.brand_list, name='brand-list'),
  path('product-list', views.product_list, name='product-list'),
  path('category-product- 
  list/<int:category_id>',views.category_product_list,name='category-product-list'),
  ]

$HTML

{% extends 'base.html' %}
{% load static %}
{% block title %}{{basic.page}}{% endblock %}
{% block content %}
<main class="container my-4 n2-fnt">
<!-- Featured Products -->
<h3 class="my-4 pb-1 mb-5 n1-anim-tit">{{basic.page}}</h3>
<div class="row">
    {% if data %}
        {% for i in data %}
        <div class="col-md-3 mb-4">
            <div class="card shadow">
            <a href="{% url 'category-product-list' category.pk=category_id %}"><img 
                 src="{{i.image.url}}" 
                 class="card-img-top" alt="{{i.title}}" /></a>
            <div class="card-body">
                <h6 class="card-text n3-fnt"><a href="{% url 'category-product-list' 
                 category.pk=category_id %}">{{i.title}}</a></h6>
            </div>
            </div>
        </div>
        {% endfor %}
    {% endif %}
  </div>
 </main>
{% endblock %}

$Error Message

TypeError at /category-product-list
category_product_list() missing 1 required positional argument: 'category_id'
Request Method: GET
Request URL:    http://127.0.0.1:8000/category-product-list
Django Version: 4.1
Exception Type: TypeError
Exception Value:    
category_product_list() missing 1 required positional argument: 'category_id'
1

There are 1 best solutions below

0
On BEST ANSWER

views.py:

category = Category.objects.get(id=category_id)

HTML page:

Instead of

{% url 'category-product-list' category.pk=category_id %}

it should be

{% url 'category-product-list' i.id %}

as it's inside a for loop and the variable in each iteration is i not category.