2

I have used Django to develop a web app. In the View function, I have rendered a queryset list to frontend. In my case, title table is book information, and Material is the detailed info of this book is attached to which course and if this attached relation is "discard". is_discard is in Material table, and not the book discard or not. In Material table several books attached to a course, and discard status is not by book but by book-course pair, as some books may discard in one course but active in other courses

view.py:

def render_2(request):
    books = Title.objects.filter(name=title).values()
    query_results_book_is_discard = 
    Material.objects.filter(id=book_id).values('is_discard')
    return render(request, 'main.html',
                              context= 
    {'query_results_book_is_discard':query_results_book_is_discard, 
      'book', books})

In the frontend, the query_results_book_is_discard variable shows the following format :

<QuerySet [{'is_discard': True}, {'is_discard': False}, {'is_discard': False}, {'is_discard': False}, {'is_discard': True}, {'is_discard': True}, {'is_discard': False}]> 

The query_results_book_is_discard variable is in a loop in frontend Django template, I want to use the forloop counter to get the value(True or False) to use if condition to check. I haved tried in main.html:

  {% for book in books %}
     {% if query_results_book_is_discard.counter0 != False %}
 ...

and

{% if query_results_book_is_discard.counter0.is_discard != False %}

and

 {% if query_results_book_is_discard.is_discard.counter0 != False %}

All failed.

How could I get the True or False value in query_results_book_is_discard to use if condition?

Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
Django
  • 67
  • 9

1 Answers1

2

I would recommend you to create a custom template tag that allows you to access a specific index of a list in the template, as follows.

Your app file tree structure should have something like this:

your_app/
    __init__.py
    models.py
    templatetags/
        __init__.py
        your_app_extras.py
    views.py

Then, in your custom template tag file.

your_app_extras.py

from django import template
register = template.Library()

@register.filter
def index(indexable, i):
    return indexable[i]

Then, in your template, load your custom template tags:

{% load your_app_extras %}

Then, in your for-loop, you use the following:

{% for book in books %}
    {% with query_results_book=query_results_book_is_discard|index:forloop.counter0 %}
        {% if query_results_book.is_discard %}
    {% endwith %}
{% endfor %}
Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
  • I already have a loop of {% for book in books %} . query_results_book_is_discard is another table only contains the discard information and book id, but do not have any book info. Here I have to use the counter of for loop instead of writing another forloop. – Django Jul 10 '21 at 05:33
  • @Django Can you share more of your code so that we can have a better look? – Bernardo Duarte Jul 10 '21 at 05:34
  • 1
    updated, in my case, is_discard is in another table, and not the book discard or not. In another table several books attached to a course, and discard status is not by book but by book-course pair, as some books may discard in one course but active in other courses – Django Jul 10 '21 at 05:43
  • 1
    How could I get is_discard value in query_results_book_is_discard? query_results_book_is_discard is like this: – Django Jul 12 '21 at 02:54
  • (query_results_book_is_discard|index:forloop.counter0).is_discard ? – Django Jul 12 '21 at 04:01
  • @Django I've updated, sorry for not answering ;D – Bernardo Duarte Jul 12 '21 at 04:26