How to index in Django Templates - django

Im trying to index a 2D list, whenever I access members.0, for example, the positioning works just fine. However I need this to change with the loop so I made a variable, but, if I was to do members.counter this wouldn't do anything.
Is there another way of doing this or is it not possible?
<tbody>
{% for item in projects %}
{% with counter=forloop.counter0 %}
<tr>
<td>{{ item }}</td>
<td>{{ members.counter }}</td>
</tr>
{% endwith %}
{% endfor %}
</tbody>

You can use the square brackets [] if you need to access a list or dict at a specific position:
<tbody>
{% for item in projects %}
{% with counter=forloop.counter0 %}
<tr>
<td>{{ item }}</td>
<td>{{ members[counter] }}</td>
</tr>
{% endwith %}
{% endfor %}
</tbody>

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>

Invalid block tag: 'edit_url', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

I'm getting this error message from django, and don't understand why. Here I use extra code i know this error comes from that. But i don't know how to solve this. Here i'm assigning id to edit button. Please help me in that.
<tbody>
{% for business_obj in business_objs %}
{% edit_url="127.0.0.1:8000/addedit/"+ business_obj.id %}
<tr>
<th scope="row">{{ business_obj.id }}</th>
<td>{{ business_obj.business_id }}</td>
<td>{{ business_obj.business_name }}</td>
<td>{{ business_obj.business_phone_number }}</td>
<td>{{ business_obj.email }}</td>
<td>{{ business_obj.business_type }}</td>
<td>{{ business_obj.address }}</td>
<td>{{ business_obj.images }}</td>
<td><i class="fas fa-edit" style="color:blue"></i></td>
<td><i class="fas fa-trash-alt" style="color:red"></i></td>
</tr>
{% endfor %}
</tbody>
You probably did not define a template tag named edit_url, but want to use a {% with ... %} template tag [Django-doc] here:
{% for business_obj in business_objs %}
{% with edit_url="127.0.0.1:8000/addedit/"|add:business_obj.id %}
...
{% endwith %}
{% endfor %}
That being said, you here should really use the {% url ... %} template tag [Django-doc] to do proper URL formatting. Something that might look like:
<i class="fas fa-edit" style="color:blue"></i>
You Can Define NameSpace in Django URLs routing file. After that, you don't need to define the domain. After that, you can refer to How to add url parameters to Django template url tag?
This will solve your problem

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 %}

Rendering dictionary having values in list format in django template

I have dictionary formate like :
uri_dict :{
"152.2.3.4" : ["/v1/draft" , 2],
"172.31.13.12" : ["/v1/url" , 34]
}
I want to render this in django template in tabular formate:
{% for keys, value in url_dict.items %}
<tr border = 1px black>
<th>{{ keys }}</th>
<td> {{ value[0] }} </td> <!--requires value[0] but not working -->
<td>{{ value[1]}} </td> <!--not working -->
</tr>
{% endfor %}
Please provide me any solution--- how to iterate over list values in template?
How to iterate over list values
There are a few options:
<td>{{ value.0 }}</td>
<td>{{ value.1 }}</td>
{% for item in value %}
<td>{{ item }}</td>
{% endfor}
These two are covered in my comment (and the other answers after it). For a list of length 2 you can also:
<td>{{ value|first }}</td>
<td>{{ value|last }}</td>
To access array elements on a django template you must refer to them as elem.attribute.
In your case, value.0 and value.1.
{% for keys, value in url_dict.items %}
<tr border = 1px black>
<th>{{ keys }}</th>
<td>{{ value.0 }}</td> <!--requires value[0] but not working -->
<td>{{ value.1 }}</td> <!--not working -->
</tr>
{% endfor %}
This page may help you: How to access array elements in a Django template?
Hope this helps,
If you always have only two items on the each value of dictionary, which are lists, then
{% for keys, value in url_dict.items %}
<tr border = 1px black>
<th>{{ keys }}</th>
<td> {{value.0}}</td>
<td> {{value.1}}</td>
</tr>
{% endfor %}
If there can be any number of items on list, then just loop for each items:
{% for keys, value in url_dict.items %}
<tr border = 1px black>
<th>{{ keys }}</th>
{% for eachval in value %}
<td> {{ eachval}}</td>
{% endfor %}
</tr>
{% endfor %}