fetch a specific item in database into Django template if it's certain type

Question:

I’ve been trying to use if statement in Django template to check if the type is equals to something in my database.

I used this code for the if statement:

{% if product.type == 'tshirt'%}
    <strong>{{product.name}}</strong>
    <span>{{product.price}}IQD</span>
{% endif %}

But it doesn’t seem to work also my back-end has no problem it can handle and load products from the database into the html template very well but the if statement is what I’m struggling with and it doesn’t seem to work like that

I just want to render the product into the template if it’s a certain type.

Asked By: Hoshmand Qadir

||

Answers:

The problem is that you are trying to compare a string with a non string type. product.type and ‘tshirt’ are different objects so format product.type to string

{% if product.type|stringformat:'s' == 'tshirt' %}
    <strong>{{product.name}}</strong>
    <span>{{product.price}}IQD</span>
{% endif %}
Answered By: Mathews Musukuma