Using while loop with counter in django template - python

I need to put a counter in while loop in template. So I did:
<tbody>
{% with count=1 %}
{% while count <={{orders_count}}: %}
{% for order in orders %}
<tr>
<td style="width:5%;"></td>
<td>{{count}}</td>
<td>{{order.name}}</td>
</tr>
{% count+=1 %}
{% endfor %}
{% endwhile %}
{% endwith %}
</tbody>
But finaly I have the following error:
Invalid block tag on line 216: 'while', expected 'endwith'. Did you forget to register or load this tag?

You do not need a while loop here, you can simply make use of the |slice template filter [Django-doc]:
<tbody>
{% for order in orders|slice:orders_count %}
<tr>
<td style="width:5%;"></td>
<td>{{ forloop.counter }}</td>
<td>{{ order.name }}</td>
</tr>
{% endfor %}
</tbody>
But slicing, etc. does not really belong in the template. You normally do that in the view.

You can try this :
<tbody>
{% for order in orders %}
<tr>
<td style="width:5%;"></td>
<td>{{forloop.counter}}</td>
<td>{{order.name}}</td>
</tr>
{% endfor %}
</tbody>

Related

If statement in table in django-templates

say we have table in template
<table class="table">
<thead>
<tr>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for student in students %}
<tr>
{% if {{student.academic_status}}=="promoted" %}
<td class=text-success>promoted</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
So is it possible using if statement in table in django-templates
Since you are already in a template tag, you should not use curly brackets ({{ … }}) for the variable, so:
{% if student.academic_status == "promoted" %}
…
{% endif %}
The correct way to add if statement in Django Templates
follow me !
if statement
{% if condition %}
{{here you add the key that you using in the views page exactly context}}
{% endif %}

Django - Looping through a dictionary using a second variable in template

I'm trying my best not to repeat myself in my code but I'm encountering a problem looping through a dictionary by key in my template.
I have two dicts:
exampledict={'firstkey':firstval, 'secondkey':secondval}
keys=['firstkey', 'secondkey']
keydict={'keys':keys}
In my template I want to loop over the exampledict using the keydict:
<tr>
{% for val in keydict %}
<td>{{ exampledict.val }}</td>
{% endfor %}
</tr>
I've noticed this kind of combination of variables doesn't work at all, I tried by using:
{'firstkey':'firstkey'}
And sending that through to the template and later trying
{{ exampledict.firstkey }}
Is there a better way to accomplish what I'm trying to do here?
Edit 1:
Manually going through each key as:
<td> {{ exampledict.firstkey }} </td> <td> {{ exampledict.secondkey }} </td>
Where firstkey and secondkey is the actual dictkey for exampledict works, although it makes for a lot of repetition.
Edit 2:
views.py
def tabletest(request):
exampledict={'firstkey':'firstval', 'secondkey': 'secondval'}
keydict={
'keys':['firstkey', 'secondkey']
}
return render(request, 'MinaFakturor/tabletest.html', {'exampledict':exampledict, 'keydict':keydict})
template
<table>
<thead>
<tr>
{% for val in keydict.keys %}
<th>{{ val }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for val in keydict.keys %}
<td>{{ exampledict.val }}</td>
{% endfor %}
</tr>
<tr>
<td>{{ exampledict.firstkey }}</td>
</tr>
</tbody>
</table>
Which produces this result:
If I remove the exampledict.firstkey term, nothing is produced in the table body.

Looping dynamic content on bootstrap4 rows [duplicate]

I would like to display data, three columns per row during my for. I would like my result to look like the following:
<table>
<tr><td>VALUE1</td><td>VALUE2</td><td>VALUE3</td></tr>
<tr><td>VALUE4</td><<td>VALUE5</td><td>VALUE6</td></tr>
</table>
Anyone know how to do it?
Syntax Error
TemplateSyntaxError at /
'for' statements should use the format 'for x in y': for i in range(0, len(all_products_list), 3)
There's a divisibleby tag.
So you can do something (ugly) like:
<table><tr>
{% for field in form %}
<td>{{ field }}</td>
{% if forloop.last %}
</tr>
{% else %}
{% if forloop.counter|divisibleby:"3" %}
</tr><tr>
{% endif %}
{% endif %}
{% endfor %}
</table>
Alternatively, you could give your form class a table_print method that returns a html string (wrapped in mark_safe).
<table>
{% for i in range(0, len(stuff), 3) %}
<tr>
{% for j in range(3) %}
<td>{{ stuff[i+j] }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Sorry misunderstood question.

Django Multiple Dictionary Parsing

I'm trying to parse the following data structure in my HTML.
{'GROUPS': {'Group1': [{'key1':'value1','key2':'value2'}, {'key1':'value3', 'key2':'value4'}], 'Group2': [{'key1':'value5','key2':'value6'}, {'key1':'value7', 'key2':'value8'}]}}
The parsing code that I have is as follows:
<tbody>
{% for group,data in data|get_value:"GROUPS" %}
<tr>
<td>{{ group }}</td>
{% for v in data.items %}
<tr>
<td>{{ v|get_value:"key1" }}</td>
<td>{{ v|get_value:"key2" }}</td>
</tr>
{% endfor %}
</tr>
{% endfor %}
</tbody>
get_value is the custom filter that I've written which basically takes the key and the data structure, and returns the value back.
But this isn't working. Can anyone help me figure out why? Thanks!
Firstly, for constant keys, you don't need a custom filter, this will work just fine:
{{ v.key1 }}
That said, data['GROUPS'] is a dict, and you want to iterate over its items, like you did with data.
data is a list though and doesn't need that:
<tbody>
{% for group, data in data.GROUPS.items %}
<tr>
<td>{{ group }}</td>
{% for v in data %}
<tr>
<td>{{ v.key1 }}</td>
<td>{{ v.key2 }}</td>
</tr>
{% endfor %}
</tr>
{% endfor %}
</tbody>

How to for-loop three columns per row in Django/python?

I would like to display data, three columns per row during my for. I would like my result to look like the following:
<table>
<tr><td>VALUE1</td><td>VALUE2</td><td>VALUE3</td></tr>
<tr><td>VALUE4</td><<td>VALUE5</td><td>VALUE6</td></tr>
</table>
Anyone know how to do it?
Syntax Error
TemplateSyntaxError at /
'for' statements should use the format 'for x in y': for i in range(0, len(all_products_list), 3)
There's a divisibleby tag.
So you can do something (ugly) like:
<table><tr>
{% for field in form %}
<td>{{ field }}</td>
{% if forloop.last %}
</tr>
{% else %}
{% if forloop.counter|divisibleby:"3" %}
</tr><tr>
{% endif %}
{% endif %}
{% endfor %}
</table>
Alternatively, you could give your form class a table_print method that returns a html string (wrapped in mark_safe).
<table>
{% for i in range(0, len(stuff), 3) %}
<tr>
{% for j in range(3) %}
<td>{{ stuff[i+j] }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Sorry misunderstood question.

Categories