How to find product name from category?

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

   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

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

{% 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 %}

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.

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:

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

I have updated the code please check

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

urls.py

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")
]

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.)

**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

You have, as examples:

<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.

Category wise

            <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

<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

Can you give an example?

You can start your research at Query string - Wikipedia

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

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

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

I did - see my previous response at How to find product name from category? - #8 by KenWhitesell

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