Furkan
Furkan

Reputation: 3

Django Template Filter With Respect to Boolean Variable

My Html

{% for category in categories %}
    <div class="row">
        <h3 style="padding-left: 15px; padding-bottom: 15px">{% filter upper %}{{ category.name }}{% endfilter %}</h3>
    </div>
    <div class="row">
    {% with products=category.product.all|is_available:True %}
    {% for product in products|slice:":4" %}



        <div class="product-width col-xl-3 col-lg-3 col-md-3 col-sm-6 col-12 mb-30">
            <div class="product-wrapper">
                <div class="product-img">
                    <a href="{% url 'shop:product' category.name product.id %}">
                        <img alt="" src="{{product.image.all.0.image.url }}">
                    </a>

                    <div class="product-action">
                        <a class="action-wishlist" href="#" title="Wishlist">
                            <i class="ion-android-favorite-outline"></i>
                        </a>
                        <a class="action-cart" href="#" title="Add To Cart">
                            <i class="ion-android-add"></i>
                        </a>

                    </div>
                </div>
                <div class="product-content text-left">

                    <div class="product-title">
                        <h4>
                            <a href="{% url 'shop:product' category.name product.id %}">{{ product.name|title }}</a>
                        </h4>
                    </div>


                    <div class="product-price-wrapper">
                        <span>{{product.price}} TL</span>

                    </div>
                </div>

            </div>
        </div>



    {% endfor %}
    {% endwith %}

    </div>


    <div class="row justify-content-end">
        <a href="{% url 'shop:category' category.name %}">Daha Fazla...</a>
    </div>




{% endfor %}

My Model

Each product has a many-to-many relation with categories and products also have an is_available variable.

class ProductCategories(models.Model):

    name = models.CharField(max_length = 60)
    image = models.ImageField(upload_to = 'ProductCategories')
    publish_date = models.DateTimeField(auto_now=False, auto_now_add=True)
    is_available = models.BooleanField()


class Product(models.Model):

    category = models.ManyToManyField(ProductCategories, related_name="product")
    name = models.CharField(max_length = 60)
    price = models.DecimalField(max_digits=65, decimal_places=2)
    description = models.TextField()
    publish_date = models.DateTimeField(auto_now=False, auto_now_add=True)
    stock_number = models.IntegerField()
    is_available = models.BooleanField()

My View

categories = ProductCategories.objects.all()

    return render(request, 'shop/shopping.html', {'categories' : categories})

I am listing 4 products under each category but I would like to filter products that are available.

Should I filter products within view class and pass to template filtered product object separate Queryset or should I apply all filters within the template?

If I should filter them within the template as I tried above, is there any way to filter product objects according to their availability?

Thanks,

Upvotes: 0

Views: 1116

Answers (2)

user10798833
user10798833

Reputation:

  1. Create a folder called "templatetags" at the same level as models.py and views.py in your application folder
  2. Create a new file with the desired name in this folder. For example : 'app_tags.py'
  3. Create a new file named __ init __.py in this folder
  4. Open app_tags.py and write this code sample for create custom template filter:
from ..models import ProductCategories
from django import template
register = template.Library()

@register.filter
def is_available(value, arg):
    products = value.filter(is_available = arg)
    return products

And use like this in your Html:

{% load app_tags %}
...
...
...
{% with products=category.product.all|is_available:True %}
...
...

Please try this solution. I hope this helps to you.

Upvotes: 0

bmons
bmons

Reputation: 3392

class ProductManager(models.Manager):

    def is_available(self):
        return self.get_queryset().filter(is_available=True)

class Product(models.Model):
        --------

        objects = ProductManager()

views.py

product = Product.objects.is_available()
return render(request, 'shop/shopping.html', {'products' : product})

templates

         {% for product in products %}
          {{ product.name }}
          {% for item in product.category.all %}
         {{ item.name }}
        {% endfor %}{% endfor %}

Upvotes: 0

Related Questions