9.2. Dynamic filtered content
To filter page content by category, we will add a condition to the store function in views.py
. When a user navigates to a category page, we retrieve the category name using category__name
and use it to filter the product query. This way, only products from the selected category are displayed, based on the category information obtained from the page's banner or URL parameters.
def store(request, category_name = None):
products = Product.objects.filter(active=True) #? grabbing all products from the database (queryset, result of the search of the database)
if category_name:
products = products.filter(category__name = category_name) #? gets the name atribute from the category class and filters it according to the name of the category obtained from the banner

The next step involves adding the hyperlinks to the banners. This will enable users to click on the banners and be redirected to the corresponding category pages.
{% for banner in banners %}
<a href="{% url 'store' banner.link_output %}">
<img src = "{{ banner.image.url}}">
</a>
{% endfor %}

Last updated