|
List Info
Thread: How do you nest two loops within a template?
|
|
| How do you nest two loops within a
template? |

|
2006-10-31 16:16:05 |
As a newbie to Python, I can't see how one can nest two
loops within a
template. Taking the template below, I have an additional
list which I
need to iterate over in order to display a single figure
for the last
cell in each table row.
Currently, if I add a for loop to the last cell (eg. {% for
b in
balance %} {} {% endfor %} ), I get all the values of
the list in
the cell - which is not want I want.
The documentation mentions a forloop.parentloop but I'm not
sure how to
apply this or whether it is the solution. Can anybody
help?
MerMer
<table>
{% for cash in transfers %}
<tr>
<td>{{ cash.date }}</td>
<td>{{ cash.source }}</td>
<td>{{ cash.credit }}</td>
<td>{{ cash.debit }}</td>
<td> {} </td>
{% endfor %}
</table>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| How do you nest two loops within a
template? |

|
2006-10-31 16:16:05 |
As a newbie to Python, I can't see how one can nest two
loops within a
template. Taking the template below, I have an additional
list which I
need to iterate over in order to display a single figure
for the last
cell in each table row.
Currently, if I add a for loop to the last cell (eg. {% for
b in
balance %} {} {% endfor %} ), I get all the values of
the list in
the cell - which is not want I want.
The documentation mentions a forloop.parentloop but I'm not
sure how to
apply this or whether it is the solution. Can anybody
help?
MerMer
<table>
{% for cash in transfers %}
<tr>
<td>{{ cash.date }}</td>
<td>{{ cash.source }}</td>
<td>{{ cash.credit }}</td>
<td>{{ cash.debit }}</td>
<td> {} </td>
{% endfor %}
</table>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| How do you nest two loops within a
template? |

|
2006-10-31 17:06:57 |
|
This is one of those things that just isn't exceptionally straightfoward to accomplish.
From within a template, and without extensions, I can't think of how you would do that immediately. It sounds like you have a relatively simple set of data, so I'd recommend instead on "tweaking" the data you have in the view prior to sending it to the template to be rendered.
Within the view, loop through your values for "b" that match up with each of the transfers, and assign a new variable (maybe "b") to each transfer as you go through the list - then you can reference it in the one loop:
{% for cash in transfers %} ... <td>{{ cash.credit }}</td> <td>{{ cash.debit }}</td>
<td>{{ cash.b }}</td> {% endfor %}
There are a couple of other references for how to display tabular data, both of which boil down to extending the tags - either with a TemplateTag or a Filter.
-joe
On 10/31/06, Merric Mercer < merric hiveonline.co.uk">merric hiveonline.co.uk> wrote:
As a newbie to Python, I can't see how one can nest two loops within a template. Taking the template below, I have an additional list which I need to iterate over in order to display a single figure for the last
cell in each table row.
Currently, if I add a for loop to the last cell (eg. {% for b in balance %} {} {% endfor %} ), I get all the values of the list in the cell - which is not want I want.
The documentation mentions a forloop.parentloop but I'm not sure how to apply this or whether it is the solution. Can anybody help?
MerMer
<table> {% for cash in transfers %} <tr>
<td>{{ cash.date }}</td> <td>{{ cash.source }}</td> <td>{{ cash.credit }}</td> <td>{{ cash.debit }}</td> <td> {} </td>
{% endfor %} </table>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users googlegroups.com To unsubscribe from this group, send email to django-users-unsubscribe googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---
|
| How do you nest two loops within a
template? |

|
2006-10-31 18:43:39 |
Joe,
My orginal code (see the inclusion tag below ) was returning
a Query
Set and an interable list. I can't understand how I can
loop through
the Query set and append the data to the end - as there is
no matching
attribute in the Query set.
I am looking to return a query set and build a table from
it. The
last column in the table is the sum of the all the previous
rows.
Here's an example.
credit Balance
50 50
40 90
50 140
Here's my orginal inclusion tag
register.inclusion_tag('cashtransfer.html')
def show_cash_transfer(username):
balance=[]
x=0
qs=CashTransfer.objects.filter(user=username)
qs1=qs[0]
for cash in qs:
x=cash.credit+x
balance.append(x)
return { 'transfers': qs, 'balance': balance }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| How do you nest two loops within a
template? |

|
2006-10-31 19:16:39 |
I found out that I needed to change the query set to a list,
then I
could append new data to the end and return this. The
following is the
ammeded inclusion tag.
register.inclusion_tag('cashtransfer.html')
def show_cash_transfer(username):
balance=[]
x=0
y=0
qs=CashTransfer.objects.filter(user=username)
item=list(qs) # convert the queery into a list
for cash in item:
x=item[y].credit+x
item[y].balance=x #as a list we are able to append
the data
y=y+1
return { 'transfers': item }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
[1-5]
|
|
|
about | contact Other archives ( Real Estate discussion Medical topics )
|