Nested for loops in templates

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Nested for loops in templates

Mikkel Kromann
Dear Django users.

Thanks to the good advice of many people on this list, I've managed to create some nice generic class based views that work simultaneously with many models.
The models may differ from each other with regards to the number of columns in their data tables as well as the naming of the columns.
In order to use the same ListView template for many models, I will need to iterate not only over the rows of the queries, but also the columns.

I managed to pass a field_list into the context_data - it works as it shows nicely in the table headers.
However, when I iterate over the rows of the query result table, I'm not able to pinpoint the fields of the data row using the field iterator.

My template.html is:
<table>
   
<tr>
{% for fields in field_list %}
       
<th>{{field}}
{% endfor %}
{% for row in row_list %}
   
<tr>
    {% for
field in field_list %}
       
<td>{{row.field}}
    {% endfor %}
{% endfor %}
</table>


Besides trying with {{row.field}}, I've tried with {{row}}.{{field}}, as well as {{ row.{{field}} }} (yeah, long shot ...)

Any ideas, or should I try to create an entirely different data structure in my view, which can be parsed more easily by Django templates?


cheers + thanks, Mikkel

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Nested for loops in templates

Vijay Khemlani
As far as I know you can't do it directly in the templating system

Yo could write a template tag as described here


or use a different data structure, for example each row as a list of the values in the order that they should appear

On Wed, Jun 13, 2018 at 4:54 PM Mikkel Kromann <[hidden email]> wrote:
Dear Django users.

Thanks to the good advice of many people on this list, I've managed to create some nice generic class based views that work simultaneously with many models.
The models may differ from each other with regards to the number of columns in their data tables as well as the naming of the columns.
In order to use the same ListView template for many models, I will need to iterate not only over the rows of the queries, but also the columns.

I managed to pass a field_list into the context_data - it works as it shows nicely in the table headers.
However, when I iterate over the rows of the query result table, I'm not able to pinpoint the fields of the data row using the field iterator.

My template.html is:
<table>
   
<tr>
{% for fields in field_list %}
       
<th>{{field}}
{% endfor %}
{% for row in row_list %}
   
<tr>
    {% for
field in field_list %}
       
<td>{{row.field}}
    {% endfor %}
{% endfor %}
</table>


Besides trying with {{row.field}}, I've tried with {{row}}.{{field}}, as well as {{ row.{{field}} }} (yeah, long shot ...)

Any ideas, or should I try to create an entirely different data structure in my view, which can be parsed more easily by Django templates?


cheers + thanks, Mikkel

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALn3ei2wQ4h-WFRy%3Dv_xqa6Bxi_Y_k8Z8qDvZpu3X2arh2ndQA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

RE: Nested for loops in templates

Matthew Pava
In reply to this post by Mikkel Kromann

field is a string – a name of a field, field is not actually an attribute of row.  You’ll need a template tag to get the attribute named field from row.

This StackOverflow question may help you:

https://stackoverflow.com/questions/844746/performing-a-getattr-style-lookup-in-a-django-template?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

 

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Mikkel Kromann
Sent: Wednesday, June 13, 2018 3:54 PM
To: Django users
Subject: Nested for loops in templates

 

Dear Django users.

 

Thanks to the good advice of many people on this list, I've managed to create some nice generic class based views that work simultaneously with many models.

The models may differ from each other with regards to the number of columns in their data tables as well as the naming of the columns.

In order to use the same ListView template for many models, I will need to iterate not only over the rows of the queries, but also the columns.

 

I managed to pass a field_list into the context_data - it works as it shows nicely in the table headers.

However, when I iterate over the rows of the query result table, I'm not able to pinpoint the fields of the data row using the field iterator.

 

My template.html is:

<table>
   
<tr>
{% for fields in field_list %}
       
<th>{{field}}
{% endfor %}
{% for row in row_list %}
   
<tr>
    {% for field in field_list %}
       
<td>{{row.field}}
    {% endfor %}
{% endfor %}
</table>


Besides trying with {{row.field}}, I've tried with {{row}}.{{field}}, as well as {{ row.{{field}} }} (yeah, long shot ...)

 

Any ideas, or should I try to create an entirely different data structure in my view, which can be parsed more easily by Django templates?

 

 

cheers + thanks, Mikkel

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0c30ad68ef05474da124726df608820b%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Nested for loops in templates

Mikkel Kromann
In reply to this post by Vijay Khemlani
Thank you Vijay.

That link seems to be a feasible path for me to take (reproduced below).

Perhaps you or others can help me with an additional question (I am a bit new to both Python and Django), that I'm struggling with:
Which type of data structure is Django iterating over in the templates (i.e. the {% for row in row_list %} in my code example above).
Is "row" the Model object or is it some sort of dictionary / array data structure?
I've been browsing through the Classy CBV ListView pages to try to get hold of that, but I was not able to clarify this myself.

thank you, Mikkel


Create a template tag like this (in yourproject/templatetags):

@register.filter
def keyvalue(dict, key):
   
return dict[key]


Usage in template:

{{dictionary|keyvalue:key_variable}}




torsdag den 14. juni 2018 kl. 00.17.26 UTC+2 skrev Vijay Khemlani:
As far as I know you can't do it directly in the templating system

Yo could write a template tag as described here

<a href="https://stackoverflow.com/questions/2894365/use-variable-as-dictionary-key-in-django-template/10700142#10700142" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F2894365%2Fuse-variable-as-dictionary-key-in-django-template%2F10700142%2310700142\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEELbTM10SigH0LQ0F0Q9QFO5BFtA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F2894365%2Fuse-variable-as-dictionary-key-in-django-template%2F10700142%2310700142\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEELbTM10SigH0LQ0F0Q9QFO5BFtA&#39;;return true;">https://stackoverflow.com/questions/2894365/use-variable-as-dictionary-key-in-django-template/10700142#10700142

or use a different data structure, for example each row as a list of the values in the order that they should appear

On Wed, Jun 13, 2018 at 4:54 PM Mikkel Kromann <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="fE4l377MBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mik...@...> wrote:
Dear Django users.

Thanks to the good advice of many people on this list, I've managed to create some nice generic class based views that work simultaneously with many models.
The models may differ from each other with regards to the number of columns in their data tables as well as the naming of the columns.
In order to use the same ListView template for many models, I will need to iterate not only over the rows of the queries, but also the columns.

I managed to pass a field_list into the context_data - it works as it shows nicely in the table headers.
However, when I iterate over the rows of the query result table, I'm not able to pinpoint the fields of the data row using the field iterator.

My template.html is:
<table>
   
<tr>
{% for fields in field_list %}
       
<th>{{field}}
{% endfor %}
{% for row in row_list %}
   
<tr>
    {% for
field in field_list %}
       
<td>{{row.field}}
    {% endfor %}
{% endfor %}
</table>


Besides trying with {{row.field}}, I've tried with {{row}}.{{field}}, as well as {{ row.{{field}} }} (yeah, long shot ...)

Any ideas, or should I try to create an entirely different data structure in my view, which can be parsed more easily by Django templates?


cheers + thanks, Mikkel

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="fE4l377MBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="fE4l377MBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django...@....
Visit this group at <a href="https://groups.google.com/group/django-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;">https://groups.google.com/group/django-users.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6d443c64-0c68-412b-9eb9-0a5640aab37d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Nested for loops in templates

Mikkel Kromann
In reply to this post by Matthew Pava
Thanks Matthew.

The field_list is a list strings containnig the field names hardcoded in Models.py by the programmer.
I.e. the field names that shoud appear in the Class Based Views.
So, perhaps this answers my question to Vijay - that the row in the iteration is in fact the Model object.
And that I can get the contents of the field specified by the contents of the "field" string using getattr()

And since (unless the programmer entered wrong values for the field names, I can use a simplified version of the filter, e.g.:


def getattribute(value, arg):
   
"""Gets an attribute of an object dynamically from a string name"""
   
if hasattr(value, str(arg)):
       
return getattr(value, arg)
   
else
       
return ""


thanks, Mikkel

torsdag den 14. juni 2018 kl. 00.19.24 UTC+2 skrev Matthew Pava:

field is a string – a name of a field, field is not actually an attribute of row.  You’ll need a template tag to get the attribute named field from row.

This StackOverflow question may help you:

<a href="https://stackoverflow.com/questions/844746/performing-a-getattr-style-lookup-in-a-django-template?utm_medium=organic&amp;utm_source=google_rich_qa&amp;utm_campaign=google_rich_qa" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F844746%2Fperforming-a-getattr-style-lookup-in-a-django-template%3Futm_medium%3Dorganic%26utm_source%3Dgoogle_rich_qa%26utm_campaign%3Dgoogle_rich_qa\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzIGi0lLv1pxcZr5EXCj_AkXnzpw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F844746%2Fperforming-a-getattr-style-lookup-in-a-django-template%3Futm_medium%3Dorganic%26utm_source%3Dgoogle_rich_qa%26utm_campaign%3Dgoogle_rich_qa\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzIGi0lLv1pxcZr5EXCj_AkXnzpw&#39;;return true;">https://stackoverflow.com/questions/844746/performing-a-getattr-style-lookup-in-a-django-template?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

 

 

From: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="O_cLONrMBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django...@... [mailto:<a href="javascript:" target="_blank" gdf-obfuscated-mailto="O_cLONrMBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django...@googlegroups.com] On Behalf Of Mikkel Kromann
Sent: Wednesday, June 13, 2018 3:54 PM
To: Django users
Subject: Nested for loops in templates

 

Dear Django users.

 

Thanks to the good advice of many people on this list, I've managed to create some nice generic class based views that work simultaneously with many models.

The models may differ from each other with regards to the number of columns in their data tables as well as the naming of the columns.

In order to use the same ListView template for many models, I will need to iterate not only over the rows of the queries, but also the columns.

 

I managed to pass a field_list into the context_data - it works as it shows nicely in the table headers.

However, when I iterate over the rows of the query result table, I'm not able to pinpoint the fields of the data row using the field iterator.

 

My template.html is:

<table>
   
<tr>
{% for fields in field_list %}
       
<th>{{field}}
{% endfor %}
{% for row in row_list %}
   
<tr>
    {% for field in field_list %}
       
<td>{{row.field}}
    {% endfor %}
{% endfor %}
</table>


Besides trying with {{row.field}}, I've tried with {{row}}.{{field}}, as well as {{ row.{{field}} }} (yeah, long shot ...)

 

Any ideas, or should I try to create an entirely different data structure in my view, which can be parsed more easily by Django templates?

 

 

cheers + thanks, Mikkel

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="O_cLONrMBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="O_cLONrMBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;"> djang...@....
Visit this group at <a href="https://groups.google.com/group/django-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;">https://groups.google.com/group/django-users.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;"> https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2bf96986-c977-49bb-b71a-6d789a1c996b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

RE: Nested for loops in templates

Matthew Pava

I would probably even simplify the code.  Apparently, there’s a setting called TEMPLATE_STRING_IF_INVALID

 

 

def getattribute(value, arg):
   
"""Gets an attribute of an object dynamically from a string name"""
    r
eturn getattr(value, arg, settings.TEMPLATE_STRING_IF_INVALID) or settings.TEMPLATE_STRING_IF_INVALID

 

 

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Mikkel Kromann
Sent: Friday, June 15, 2018 6:36 AM
To: Django users
Subject: Re: Nested for loops in templates

 

Thanks Matthew.

 

The field_list is a list strings containnig the field names hardcoded in Models.py by the programmer.

I.e. the field names that shoud appear in the Class Based Views.

So, perhaps this answers my question to Vijay - that the row in the iteration is in fact the Model object.

And that I can get the contents of the field specified by the contents of the "field" string using getattr()

 

And since (unless the programmer entered wrong values for the field names, I can use a simplified version of the filter, e.g.:

 

 

def getattribute(value, arg):
   
"""Gets an attribute of an object dynamically from a string name"""
   
if hasattr(value, str(arg)):
       
return getattr(value, arg)
   
else
       
return ""


thanks, Mikkel

 

torsdag den 14. juni 2018 kl. 00.19.24 UTC+2 skrev Matthew Pava:

field is a string – a name of a field, field is not actually an attribute of row.  You’ll need a template tag to get the attribute named field from row.

This StackOverflow question may help you:

https://stackoverflow.com/questions/844746/performing-a-getattr-style-lookup-in-a-django-template?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

 

 

From: <a href="javascript:" target="_blank">django...@... [mailto:<a href="javascript:" target="_blank">django...@...] On Behalf Of Mikkel Kromann
Sent: Wednesday, June 13, 2018 3:54 PM
To: Django users
Subject: Nested for loops in templates

 

Dear Django users.

 

Thanks to the good advice of many people on this list, I've managed to create some nice generic class based views that work simultaneously with many models.

The models may differ from each other with regards to the number of columns in their data tables as well as the naming of the columns.

In order to use the same ListView template for many models, I will need to iterate not only over the rows of the queries, but also the columns.

 

I managed to pass a field_list into the context_data - it works as it shows nicely in the table headers.

However, when I iterate over the rows of the query result table, I'm not able to pinpoint the fields of the data row using the field iterator.

 

My template.html is:

<table>
   
<tr>
{% for fields in field_list %}
       
<th>{{field}}
{% endfor %}
{% for row in row_list %}
   
<tr>
    {% for field in field_list %}
       
<td>{{row.field}}
    {% endfor %}
{% endfor %}
</table>


Besides trying with {{row.field}}, I've tried with {{row}}.{{field}}, as well as {{ row.{{field}} }} (yeah, long shot ...)

 

Any ideas, or should I try to create an entirely different data structure in my view, which can be parsed more easily by Django templates?

 

 

cheers + thanks, Mikkel

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank">django-users...@....
To post to this group, send email to <a href="javascript:" target="_blank">djang...@....
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c9e29d66-f93c-4fc4-ae9f-dbeae93a2e45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2bf96986-c977-49bb-b71a-6d789a1c996b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/668f062779e5458e8da0e5377197cca1%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.