Django indexing in template - django

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.

Related

How to index in Django Templates

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>

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

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

Django templates: How to access a form field equal to a variable value?

Imagine a following use case: I have a dictionary of report templates. Each report template has an ID and a name. It looks more or less like this:
reports = [ { 'ID' : 'A1',
'NAME' : 'special report 1 for department A',
... (other fields irrelevant to the question's matter)
},
{ 'ID' : 'X9',
'NAME' : 'special report 9 for department X',
... (other fields irrelevant to the question's matter)
},
]
Access to and visibility of the reports are to be stored in a database.
I created a form with field names equal to reports' IDs. And everything goes fine until the moment I have to show the form in the template. I don't know how to access the proper form's field. This syntax doesn't work:
{% for report in reports %}
<tr>
<td>{{ report.ID }}</td>
<td>{{ report.NAME }}</td>
<td>{{ form.report.ID }}</td>
</tr>
{% endfor %}
Also:
{% for report in reports %}
<tr>
<td>{{ report.ID }}</td>
<td>{{ report.NAME }}</td>
{% with report.ID as key %}
<td>{{ form.key }}</td>
{% endwith %}
</tr>
{% endfor %}
doesn't work.
How should the syntax look like? What should be placed instead of the question marks in the code below?
{% for report in reports %}
<tr>
<td>{{ report.ID }}</td>
<td>{{ report.NAME }}</td>
<td>{{ form.??? }}</td>
</tr>
{% endfor %}
I crossed upon this solution: Django Templates: Form field name as variable? and I guess I'll have to use it if there won't be any other way to solve my problem.
It's discouraged because the templates are supposed to be as without logic as possible. But, in practice, this comes up all the time. You can make a template filter that does this:
#register.filter(name='get')
def get(o, index):
try:
return o[index]
except:
return settings.TEMPLATE_STRING_IF_INVALID
Then, in the template, after you load the templatetags library appropriately:
{% for report in reports %}
<tr>
...
<td>{{ form|get:report.ID }}</td>
</tr>
{% endfor %}
Try this:
{{ form.fields.key }}