Home > Net >  How to filter data in django template?
How to filter data in django template?

Time:10-14

i have a django model as follows in which i defined categories

CATEGORY_CHOICES = (
('BS', 'Best Selling'),
('TP', 'Trending Product'),
('RP', 'Related Products'),
('NA', 'New Arrival'),
('F', 'Featured'),
('OS', 'on sale'),)

class Item(models.Model):
     title = models.CharField(max_length=100)
     price = models.FloatField()
     discount_price = models.FloatField(blank=True, null=True)
     category = models.CharField(choices=CATEGORY_CHOICES, max_length=2)
     label = models.CharField(choices=LABEL_CHOICES, max_length=1)
     slug = models.SlugField()
     description = models.TextField()
     image = models.ImageField()

     def __str__(self):
       return self.title

     def get_absolute_url(self):
        return reverse("core:product", kwargs={
        'slug': self.slug
           })

     def get_add_to_cart_url(self):
        return reverse("core:add-to-cart", kwargs={
        'slug': self.slug
         })

     def get_remove_from_cart_url(self):
         return reverse("core:remove-from-cart", kwargs={
        'slug': self.slug
         })

**and in my Django home page there are multiples sections based on categories like trending product, on sale, featured, new arrivals etc ** what I wanted is to filter data based on that categories and show to the respective section and for that, I registred a template as follows:

@register.filter

def in_category(Item, category): return Item.filter(category=category)

and on my home template i tried to use this filter as follows:

{% for object_list in object_list|in_category:Featured %}

            <div class="col-3">
              <div class="custom-col-5">
                <div class="single_product">
                  <div class="product_thumb">
                    <a href="{{ item.get_absolute_url }}" class="primary_img"><img src="{{ item.image.url }}"
                        alt="product1"></a>
                    <a href="{{ item.get_absolute_url }}" class="secondary_img"><img src="{{ item.image.url }}"
                        alt="product1"></a>
                    <div class="quick_button">
                      <a href="{{ item.get_absolute_url }}" data-toggle="modal" data-target="#modal_box"
                        data-placement="top" data-original-title="quick view">Quick
                        View</a>
                    </div>
                  </div>
                  <div class="product_content">
                    <div class="tag_cate">
                      <a href="#">Ring, Necklace,</a>
                      <a href="#">Earrings</a>
                    </div>
                    <h3><a href="{{ item.get_absolute_url }}">{{ item.title }}</a></h3>
                    <div class="price_box">
                      <span class="old_price">Rs. 45654</span>
                      <span class="current_price">{% if item.discount_price %}
                        {{ item.discount_price }}
                        {% else %}
                        {{ item.price }}
                        {% endif %}</span>
                    </div>
                    <div class="product_hover">
                      <div class="product_ratings">
                        <ul>
                          <li><a href="#"><i class="ion-ios-star-outline"></i></a>
                          </li>
                          <li><a href="#"><i class="ion-ios-star-outline"></i></a>
                          </li>
                          <li><a href="#"><i class="ion-ios-star-outline"></i></a>
                          </li>
                          <li><a href="#"><i class="ion-ios-star-outline"></i></a>
                          </li>
                          <li><a href="#"><i class="ion-ios-star-outline"></i></a>
                          </li>
                        </ul>
                      </div>
                      <div class="product_desc">
                        <p>This is a gold ring with diamond and Lorem ipsum
                          dolor sit amet.</p>
                      </div>
                      <div class="action_links">
                        <ul>
                          <li><a href="#" data-placement="top" title="Add to Wishlist" data-toggle="tooltip"><span
                                class="ion-heart"></span></a></li>
                          <li class="add_to_cart"><a href="#" title="Add to Cart">Add
                              to Cart</a></li>
                          <li><a href="#" title="Compare"><i class="ion-ios-settings-strong"></i></a>
                          </li>
                        </ul>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            {% endfor %}

but django says it is an error VariableDoesNotExist at / Failed lookup for key [Featured] in

can sombody help me with this thanks in advance.

CodePudding user response:

You can use simple for loop and condition

{% if item.category == "Featured" %}
---------html for featured categ---
{% endif %}
{% if item.category == "onsale" %}
---------html for onsale categ---
{% endif %}
 .
 .
 .
{% endfor %}```
  • Related