Home > Software design >  Django issue, Categories Page can't be uploaded with products in it
Django issue, Categories Page can't be uploaded with products in it

Time:09-06

$ 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 >
<!-- Featured Products -->
<h3 >{{basic.page}}</h3>
<div >
    {% if data %}
        {% for i in data %}
        <div >
            <div >
            <a href="{% url 'category-product-list' category.pk=category_id %}"><img 
                 src="{{i.image.url}}" 
                  alt="{{i.title}}" /></a>
            <div >
                <h6 ><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'

CodePudding user response:

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.

  • Related