Hi everyone I'm struggling with a for loop in attempt to make a categories filter (Django - Bootstrap)

Viewed 36

Like the title say's I have a problem trying to make a categories filter, already did one for my Blog because only has a couple of categories without subcategories, but this one is different because it has categories and subcategories with one to many fields and already tried multiples approaches but I couldn't make it work. I would be glad if someone lend me a hand.

This one was my last attempt, I tried to make it like my other less complex categories filter.

Store unique html template.

<section class="page-section cta rounded p-6 p-md-5 m-md-4 text-center bg-light">
    <div class="container">
      <div class="row">
        <div class="col-xl-9 text-center mx-auto">
            <h2 class="section-heading mb-4">
              <span class="section-heading-lower">Horizontal</span>
            </h2>

            <div style="padding-bottom: 1%; padding-top: 3%;">
              <h4><span class="section-heading-upper">CATEGORIAS: </span></h4>

              {% for itemWide in itemsWide %}
                {% for category in itemWide.categories.all %}

                <a type="button" class="btn btn-dark" href="{% url 'storePacks' %}">
                <span class="section-heading-upper" style="font-size:small;">{{category.name}}</span>
                </a>

                {% endfor %}
              {% endfor %}

            </div>

        </div>
      </div>
    </div>
</section>

Store model

class ItemCategory(models.Model):

    name = models.CharField(max_length=50)
    created = models.DateField(auto_now_add=True)
    updated = models.DateField(auto_now_add=True)

    class Meta:

        verbose_name = 'itemCategory'
        verbose_name_plural = 'itemsCategories'

    def __str__(self):

        return self.name

class SubCategoryVert(models.Model):

    parentCategory = models.ForeignKey(ItemCategory, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)
    created = models.DateField(auto_now_add=True)
    updated = models.DateField(auto_now_add=True)

    class Meta:

        verbose_name = 'SubCategoryVert'
        verbose_name_plural = 'SubCategoriesVert'

    def __str__(self):

        return self.name

class SubCategoryWide(models.Model):

    parentCategory = models.ForeignKey(ItemCategory, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)
    created = models.DateField(auto_now_add=True)
    updated = models.DateField(auto_now_add=True)

    class Meta:

        verbose_name = 'SubCategoryWide'
        verbose_name_plural = 'SubCategoriesWide'

    def __str__(self):

        return self.name

class ItemVert(models.Model):

    name = models.CharField(max_length=50)
    categories = models.ForeignKey(SubCategoryVert, on_delete=models.CASCADE)
    detail = models.CharField(max_length=50, null=True, blank=True)
    img = models.ImageField(upload_to='store/img/Vert', null=True, blank=True)
    price = models.FloatField()
    stock = models.BooleanField(default=True)
    created = models.DateField(auto_now_add=True)
    updated = models.DateField(auto_now_add=True)

    class Meta:

        verbose_name = 'itemVert'
        verbose_name_plural = 'itemsVert'

class ItemWide(models.Model):

    name = models.CharField(max_length=50)
    categories = models.ForeignKey(SubCategoryWide, on_delete=models.CASCADE)
    detail = models.CharField(max_length=50, null=True, blank=True)
    img = models.ImageField(upload_to='store/img/Wide', null=True, blank=True)
    price = models.FloatField()
    stock = models.BooleanField(default=True)
    created = models.DateField(auto_now_add=True)
    updated = models.DateField(auto_now_add=True)

    class Meta:

        verbose_name = 'itemWide'
        verbose_name_plural = 'itemsWide'

If is needed store url

urlpatterns = [

    path('', views.store, name='store'),
    path('packs/', views.storePacks, name='storePacks'),
    path('unico/', views.storeUnique, name='storeUnique'),
]
0 Answers
Related