using boolan in django templates - django

for item in query_results:
num +=1
print num
this will give you 1, 2, 3, 4 etc
I then tried doing this in django templates doing the following
{% for item in query_results %}
<tr>
<td>{{ item.user }}</td>
<td>{{ num|add:"1" }}</td>
</tr>
{% endfor %}
But this only returns 1, 1, 1, 1, 1 etc. This says to me that the 1 isn't being saved to num each cycle. IS this then not a capability of django templates, or am i just doing it wrong.

Use forloop.counter instead.
{% for item in query_results %}
<tr>
<td>{{ item.user }}</td>
<td>{{ forloop.counter }}</td>
</tr>
{% endfor %}

The built-in add filter just adds the argument to the value, but doesn't modify it. That's why you're getting always 1 as result.
More about it: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#add

Related

how to get random 1/2 data from database in jinj2 template in Django?

<table>
{% for field in fields %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% endfor %}
</table>
here we will get all the data from fields . but i want to only get randomly 1/2 (i can specified how many) data in jinja2 template from backend ?
How to do this ?
<table>
{% for field in fields %}
{% if forloop.counter < x %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
just put your desired number in x,you are good to go.
Try this. You can add this logic to the frontend. This way it will display records with even ID. and those will 1/2 as well.
<table>
{% for field in fields %}
<tr>
{% if field.id%2 == 0 %}
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
{% endif %}
</tr>
{% endfor %}
</table>

Using a key from a for loop in django

I have an array of arrays that I am trying to put into a table. It's something that works easily in PHP but I can't seem to figure out how to do it in Django. I want to do something like this:
{% for o in test %}
<tr>
<td>{{ test.row{{o}}.test1 }}</td>
<td>{{ test.row{{o}}.test2 }}</td>
<td>{{ test.row{{o}}.test3 }}</td>
</tr>
{% endfor %}
for an array like this :
random_array = [[1, 2, 3],[4, 5, 6]]
in python you should use
array[1][0] to acces the number 4
in django templating language it goes like this :
{{ random_array.1.0 }}
update :
<table>
{% for row in array %}
<tr>
{% for i in row %}
<td>{{ i }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>

How to add two for loops in a single table body in django template

<thead>
<th> username </th>
<th>place</th>
</thead>
{% for i, j in user_group1, user_group2 %}
<tbody>
{% if i %}
<td>{{ i.username }}</td>
<td>{{ i.place }}</td>
{% else %}
<td>{{ j.username }}</td>
<td>{{ j.place }}</td>
{% endif %}
</tbody>
{% endfor %}
I want to use two for loops in a sinle table body. First i need to start the first one and after that i need to start the next one. how can i do this
If you're using Jinja2, you can join the two lists into one with the + operator:
{% for i in user_group1|list + user_group2|list %}
<tbody>
<td>{{ i.username }}</td>
<td>{{ i.place }}</td>
</tbody>
{% endfor %}

Django Template : Combine 2 loops over different lists

I'm wondering how I can combine these 2 querysets in my template in order to loop over them.
requests = Download.objects.values('pub__age_id').annotate(count=Count('pub__age_id'))
max_download_number = Download.objects.values('pub__age_id').annotate(max_usage=Max('usage'))
context = {'requests': requests, 'max_download_number': max_download_number}
In my template :
{% for item in requests %}
{% for element in max_download_number %}
<tr>
<td>{{ item.pub__age_id }}</td>
<td><span class="badge alert-info">{{ item.count }}</span></td>
<td>{{ element.max_usage }}</td>
<td>Something</td>
</tr>
{% endfor %}
{% endfor %}
It displays wrong loops :
Why don't you combine it in the view:
requests = Download.objects.values('pub__age_id').annotate(count=Count('pub__age_id')).annotate(max_usage=Max('usage'))
and then in the template:
<td>{{ item.pub__age_id }}</td>
<td><span class="badge alert-info">{{ item.count }}</span></td>
<td>{{ item.max_usage }}</td>

Django indexing in template

I have the following section in my template where I make use of indexing my query set to access its fields. I know this can be done without indexing but I am trying whether this can be done by indexing. confirmed_length is a list which is [0] and confirmed_bookings is the query_set
{% for i in confirmed_length %}
<tr>
<th scope="row">{{ confirmed_bookings.0.booking_id }}</th>
<td>{{ i }}</td>
<td>{{ confirmed_bookings.0.booking_date }}</td>
<td>{{ confirmed_bookings.i.room_id }}</td>
</tr>
{% endfor %}
where {{ i }} shows 0 but nothing shows for others except
{{ confirmed_bookings.0.booking_date }} and {{ confirmed_bookings.0.booking_id }}. Indexing with 0 shows me the details but indexing with i (where i is an integer and its value is 0) does not show anything.