# How to find product name from category?

**URL:** <https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845>\
**Category:** Templates & Frontend\
**Created:** [June 25, 2023, 10:12am UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845 "2023-06-25T10:12:18Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahir0304](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mahir0304/32/14436_2.png) [@mahir0304](https://forum.djangoproject.com/u/mahir0304)\
**Post date:** [June 25, 2023, 10:12am UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/1 "2023-06-25T10:12:18Z")

</div>

I have created three models Main Category , Category and Product Category model contains the Foreign Key of main category and product model contains the Foreign Key of main category and category model Product model contains the product details.

**models.py**

```auto
   class BaseModel(models.Model):
        id = models.UUIDField(primary_key=True, unique=True,
                            editable=False, default=uuid.uuid4)
        created_at = models.DateTimeField(auto_now_add=True)

        class Meta:
            abstract = True

    class MainCategory(BaseModel):
        name = models.CharField(max_length=100)

        def __str__ (self):
            return self.name

    class Category(BaseModel):
        main_category = models.ForeignKey(
            MainCategory, on_delete=models.CASCADE, null=True, blank=True)
        name = models.CharField(max_length=100)

        def __str__ (self):
            return self.main_category.name + "---" + self.name

class Product(BaseModel):
        name = models.CharField(max_length=100)
        description = models.TextField()

        main_category = models.ForeignKey(
            MainCategory, on_delete=models.CASCADE, related_name='main_category')

        category = models.ForeignKey(
            Category, on_delete=models.CASCADE, related_name='category', null=True, 
            blank=True)            

        slug = models.SlugField(unique=True, max_length=100, null=True, blank=True)

        def save(self, *args, **kwargs):
            self.slug = generate_slug(self.name)
            super(Product, self).save(*args, **kwargs)

```

**Views.py**

```auto
def categories(request):
    main_categories = MainCategory.objects.all()
    context = {
        'main_categories': main_categories
    }
    return render(request, 'allProducts.html', context)

def showProductsMainCategoryWise(request):
    main_category = request.GET.get('main_category')

    if main_category == None:
        products = Product.objects.order_by('id')

    else:
        products = Product.objects.filter(main_category__name=main_category)
    main_categories = MainCategory.objects.all()
    context = {
        'products': products,
        'main_categories': main_categories
    }
    return render(request, 'allProducts.html', context)

def showProductsCategoryWise(request):
    main_category = request.GET.get('main_category')
    category = request.GET.get('category')

    if main_category == None and category == None:
        products = Product.objects.order_by('id')
    else:
        products = Product.objects.all().filter(
            Q(main_category__name=main_category), Q(category__name=category))
    main_categories = MainCategory.objects.all()
    categories = Category.objects.all()

    context = {
        'categories': categories,
        'products': products,
        'main_categories': main_categories
    }
    return render(request, 'allProducts.html', context)

```

**product.html**

```auto
{% extends 'base.html' %}
{% block body %}
<div class="container my-5">
    <div class="row">
        <div class="col-sm-4">
            <div class="card mx-2" style="width: 18rem;">
                <div class="card-header text-center">
                    <a href="{% url 'showProductsMainCategoryWise' %}">All Product</a>
                </div>
                <ul id="myUL">
                    {% for i in main_categories %}
                    <li>
                        <span class="caret">
                            <a href="{% url 'showProductsMainCategoryWise' %}?main_category={{i.name}}">{{i.name}}</a>
                        </span>
                        <ul class="nested">
                            {% for cat in i.category_set.all %}
                            <a
                                href="{% url 'showProductsCategoryWise' %}?main_category={{cat.main_category.name}} and category={{cat.name}}">
                                <li>{{cat.name}}</li>
                            </a>
                            {% endfor %}
                        </ul>
                    </li>
                    {% endfor %}
                </ul>
            </div>
        </div>
        <div class="col-sm-8">
            <table class="table">
                <thead>
                    <tr>
                        <th scope="col">S/N</th>
                        <th scope="col">Produc Name</th>
                    </tr>
                </thead>
                {% if products %}
                <tbody>
                    {% for p in products %}
                    <tr>
                        <th scope="row">{{forloop.counter}}</th>
                        <td>
                            <a href="{{p.slug}}">
                                {{p.name}}
                            </a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
                {% else %}
                <p>Comming Soon...</p>
                {% endif %}
            </table>
        </div>
    </div>
</div>
{% endblock body %}

```

 ![product](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/0/0db434f151b7688f452444921b5ad7a13abf95b3.jpeg)

According to the image above  
If you click on all product then all the products are showing and if you click on women fashion, men’s fashion TV, mobiles then they are showing the products according to that main category but when you click on women clothing nothing is showing

So please tell me if showProductsCategoryWise method and product.html file is correct or where something is wrong or something else needs to be added.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 25, 2023, 11:52am UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/2 "2023-06-25T11:52:09Z")

</div>

Side note: When posting code or templates here, please surround the code (or template) between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing your original post for this.)

Please be more specific and provide more details about the problem.

If you’re having a problem with a specific view, which view is the problem?

What are you not seeing that you’re expecting to see? (Or what are you seeing that is unexpected?)

Are you getting any kind of error message? (If so, please post it with the complete traceback.)

I do notice that in your view `showProductsMainCategoryWise` you have:

> [@mahir0304](#):
>
> ```auto
> else:
> products = Product.objects.filter(main_category__name=main_category)
> 
> ```

It appears here that the line under the `else` statement is not indented.

---

<div class="post-metadata">

**Author:** ![mahir0304](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mahir0304/32/14436_2.png) [@mahir0304](https://forum.djangoproject.com/u/mahir0304)\
**Post date:** [June 26, 2023, 5:00pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/3 "2023-06-26T17:00:51Z")

</div>

I have updated the code please check

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 26, 2023, 5:10pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/4 "2023-06-26T17:10:05Z")

</div>

Please identify what URLs are being submitted when each of “Women Fashion”, “Mens Fashion”, and “Women Clothing” menu items are selected.

---

<div class="post-metadata">

**Author:** ![mahir0304](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mahir0304/32/14436_2.png) [@mahir0304](https://forum.djangoproject.com/u/mahir0304)\
**Post date:** [June 26, 2023, 6:00pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/5 "2023-06-26T18:00:19Z")

</div>

**urls.py**

```auto
from django.urls import path
from .import views
urlpatterns = [
    path('', views.categories, name="categories"),
    path('showProductsMainCategoryWise', views.showProductsMainCategoryWise,
         name='showProductsMainCategoryWise'),
    path('showProductsCategoryWise', views.showProductsCategoryWise,
         name='showProductsCategoryWise'),
    path('<str:slug>', views.showProductDetails, name="showProductDetails")
]

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 26, 2023, 9:04pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/6 "2023-06-26T21:04:59Z")

</div>

Good, that’s helpful, but not what I was asking for.

Those menu links should result in an http request being generated. I’m looking to find out what the urls are associated with those menu entries as they were rendered in the browser. (Use your browser’s developer tools and examine the menu. Find the `<a>` tags for those entries and report the `href` values for them.)

---

<div class="post-metadata">

**Author:** ![mahir0304](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mahir0304/32/14436_2.png) [@mahir0304](https://forum.djangoproject.com/u/mahir0304)\
**Post date:** [June 27, 2023, 5:22pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/7 "2023-06-27T17:22:25Z")

</div>

```auto
**product category**
<div class="col-sm-4">
            <div class="card mx-2" style="width: 18rem;">
                <div class="card-header text-center">
                    <a href="/showProductsMainCategoryWise">All Product</a>
                </div>
                <ul id="myUL">
                    
                    <li>
                        <span class="caret">
                            <a href="/showProductsMainCategoryWise?main_category=Women Fashion">Women Fashion</a>
                        </span>
                        <ul class="nested">
                            
                            <a href="/showProductsCategoryWise?main_category=Women Fashion and category=Women Clothing">
                                <li>Women Clothing</li>
                            </a>
                            
                            <a href="/showProductsCategoryWise?main_category=Women Fashion and category=Accessories">
                                <li>Accessories</li>
                            </a>
                            
                            <a href="/showProductsCategoryWise?main_category=Women Fashion and category=Women Shoes">
                                <li>Women Shoes</li>
                            </a>
                            
                        </ul>
                    </li>
                    
                    <li>
                        <span class="caret">
                            <a href="/showProductsMainCategoryWise?main_category=Mens Fashion">Mens Fashion</a>
                        </span>
                        <ul class="nested">
                            
                        </ul>
                    </li>
                    
                    <li>
                        <span class="caret">
                            <a href="/showProductsMainCategoryWise?main_category=TV">TV</a>
                        </span>
                        <ul class="nested">
                            
                        </ul>
                    </li>
                    
                    <li>
                        <span class="caret">
                            <a href="/showProductsMainCategoryWise?main_category=Mobiles,Computer">Mobiles,Computer</a>
                        </span>
                        <ul class="nested">
                            
                        </ul>
                    </li>
                    
                </ul>
            </div>
        </div>

```

`I think this is enough or more is needed`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 27, 2023, 5:27pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/8 "2023-06-27T17:27:35Z")

</div>

You have, as examples:

```auto
<a href="/showProductsMainCategoryWise?main_category=Women Fashion">

<a href="/showProductsCategoryWise?main_category=Women Fashion and category=Women Clothing">

```

These are not valid query variables in the url.

You must not have spaces within the `href`, they’re not going to get passed through to the server.

One of your options would be to URL-encode those variables, and then decode them on the server.

---

<div class="post-metadata">

**Author:** ![mahir0304](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mahir0304/32/14436_2.png) [@mahir0304](https://forum.djangoproject.com/u/mahir0304)\
**Post date:** [June 27, 2023, 5:29pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/9 "2023-06-27T17:29:44Z")

</div>

**Category wise**

```auto
            <div class="card mx-2" style="width: 18rem;">
                <div class="card-header text-center">
                    <a href="/showProductsMainCategoryWise">All Product</a>
                </div>
                <ul id="myUL">
                    
                    <li>
                        <span class="caret">
                            <a href="/showProductsMainCategoryWise?main_category=Women Fashion">Women Fashion</a>
                        </span>
                        <ul class="nested">
                            
                            <a href="/showProductsCategoryWise?main_category=Women Fashion and category=Women Clothing">
                                <li>Women Clothing</li>
                            </a>
                            
                            <a href="/showProductsCategoryWise?main_category=Women Fashion and category=Accessories">
                                <li>Accessories</li>
                            </a>
                            
                            <a href="/showProductsCategoryWise?main_category=Women Fashion and category=Women Shoes">
                                <li>Women Shoes</li>
                            </a>
                            
                        </ul>
                    </li>
                    
                    <li>
                        <span class="caret">
                            <a href="/showProductsMainCategoryWise?main_category=Mens Fashion">Mens Fashion</a>
                        </span>
                        <ul class="nested">
                            
                        </ul>
                    </li>
                    
                    <li>
                        <span class="caret">
                            <a href="/showProductsMainCategoryWise?main_category=TV">TV</a>
                        </span>
                        <ul class="nested">
                            
                        </ul>
                    </li>
                    
                    <li>
                        <span class="caret">
                            <a href="/showProductsMainCategoryWise?main_category=Mobiles,Computer">Mobiles,Computer</a>
                        </span>
                        <ul class="nested">
                            
                        </ul>
                    </li>
                    
                </ul>
            </div>
        </div>

```

**product list**

```auto
<div class="col-sm-8">
            <table class="table">
                <thead>
                    <tr>
                        <th scope="col">S/N</th>
                        <th scope="col">Produc Name</th>
                    </tr>
                </thead>
                {% if products %}
                <tbody>
                    {% for p in products %}
                    <tr>
                        <th scope="row">{{forloop.counter}}</th>
                        <td>
                            <a href="{{p.slug}}">
                                {{p.name}}
                            </a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
                {% else %}
                <p>Comming Soon...</p>
                {% endif %}
            </table>
        </div>

```

I think this is enough or more is needed

---

<div class="post-metadata">

**Author:** ![mahir0304](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mahir0304/32/14436_2.png) [@mahir0304](https://forum.djangoproject.com/u/mahir0304)\
**Post date:** [June 27, 2023, 5:31pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/10 "2023-06-27T17:31:38Z")

</div>

Can you give an example?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 27, 2023, 5:33pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/11 "2023-06-27T17:33:05Z")

</div>

You can start your research at [Query string - Wikipedia](https://en.wikipedia.org/wiki/Query_string)

---

<div class="post-metadata">

**Author:** ![mahir0304](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mahir0304/32/14436_2.png) [@mahir0304](https://forum.djangoproject.com/u/mahir0304)\
**Post date:** [June 27, 2023, 5:35pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/12 "2023-06-27T17:35:10Z")

</div>

Is the views function correct or something else needs to be added to it?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 27, 2023, 5:39pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/13 "2023-06-27T17:39:39Z")

</div>

No easy way to tell yet, it’s not being called properly.

---

<div class="post-metadata">

**Author:** ![mahir0304](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mahir0304/32/14436_2.png) [@mahir0304](https://forum.djangoproject.com/u/mahir0304)\
**Post date:** [June 27, 2023, 5:41pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/14 "2023-06-27T17:41:48Z")

</div>

I tried a lot, can you tell me where I am going wrong

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 27, 2023, 5:44pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/15 "2023-06-27T17:44:25Z")

</div>

I did - see my previous response at [How to find product name from category? - #8 by KenWhitesell](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/8)

---

<div class="post-metadata">

**Author:** ![mahir0304](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mahir0304/32/14436_2.png) [@mahir0304](https://forum.djangoproject.com/u/mahir0304)\
**Post date:** [June 27, 2023, 5:53pm UTC](https://forum.djangoproject.com/t/how-to-find-product-name-from-category/21845/16 "2023-06-27T17:53:50Z")

</div>

Is there something wrong in urls file or something wrong in views file or something wrong in html file
