django to change the value of varible in template based on the number

330 Views Asked by At

there is a model books

where i want to fetch all the objects and display it to user using django templates

books=books.objects.all()

and with django template tag im able to fetch the values in the model and display them

but is there any way where django can change the variable values based on the number

for example this part :

 {% for b in books %}
    <tr>
    <td>{{b.books_id}}</td>
    </tr>
  {% endfor %}

this will display all the id's
but is there any way to override this in django

Lets say

for instance ,if the b.books_id for any object is 1 then it should display something like "completed"

if its 2 then it should display something like "no"

is there any possibility to do that ?? using if or for loops in django template tags or through views

1

There are 1 best solutions below

0
Renaud On

Yes tout have if statement in Django templates (replace b.books_id by name of the objects which contains 1 or 2):

{% for b in books %}
    {% if b.books_id == "1" %}
        Completed
    {% elif b.books_id == "2" %}
        No
    {% else %}
        ???
    {% endif %}
{% endfor %}