using forloop.counter to access data

271 views
Skip to first unread message

Larry Martell

unread,
Mar 21, 2012, 8:47:23 AM3/21/12
to django...@googlegroups.com
This is probably a stupid newbie question ....

I want to access a column of data in a row using forloop.counter, but
I cannot get it to work.

In my test code, if I display {{ forloop.counter }} I get 2
If I display {{ headers.0.2 }} I get ToolType
But if I display {{ headers.0.forloop.counter }} I get nothing

What is the proper syntax for this?

James

unread,
Mar 21, 2012, 9:53:24 AM3/21/12
to django...@googlegroups.com
the forloop.counter and friends will simply give you information about the iteration, it won't give you any information about the data in the queryset (I'm assuming this is what you mean when you say column).


 If you want to access the column of data, assuming it's referenced from a model, just the attribute of that model. If you passed in a list to the template, you just use the django "dot" look-up syntax.


So, let's say your list looks like a = [1,2,3,4] and in your context it looks like c ={'a':a}

if you want to reference the second "column" (index really) you would just do {{ a.1 }}, this will render the number "2".

You would use forloop.counter for something like, let's say we passed in hundreds of such lists. And, for whatever reason, once we reach the 100th iteration, we want the loop to end.

Then we would do this:

{% for list in list_of_lists %}
  {{ list.1 }}
{% if forloop.counter == 100 %}
    {% endfor %}
{% endif %}
{% if forloop. last %} 
    {% endfor %}
{% endif %} 

The last if / endif is not necessary, but I wanted to show some control flow using the forloop context. In fact, I wouldn't do this at all at the template level. I would instead limit the queryset to 100 when creating the queryset and pass that in and then just iterate over it like any other queryset, but I think you see what I mean.

Tom Evans

unread,
Mar 21, 2012, 10:32:48 AM3/21/12
to django...@googlegroups.com

The preferred solution is to structure your data better in your view
so that writing the template to render the data is straightforward.

The pragmatic solution is to use a custom filter to access your
current data structures:

http://stackoverflow.com/questions/2024660/django-sort-dict-in-template

Cheers

Tom

Larry Martell

unread,
Mar 21, 2012, 10:41:08 AM3/21/12
to django...@googlegroups.com
On Wed, Mar 21, 2012 at 7:53 AM, James <eire...@gmail.com> wrote:
>
>
> On Wednesday, March 21, 2012 8:47:23 AM UTC-4, Larry....@gmail.com wrote:
>>
>> This is probably a stupid newbie question ....
>>
>> I want to access a column of data in a row using forloop.counter, but
>> I cannot get it to work.
>>
>> In my test code, if I display {{ forloop.counter }} I get 2
>> If I display {{ headers.0.2 }} I get ToolType
>> But if I display {{ headers.0.forloop.counter }} I get nothing
>>
>> What is the proper syntax for this?
>
>
> the forloop.counter and friends will simply give you information about the
> iteration, it won't give you any information about the data in the queryset

Yes, I realize that. I am using it withing a loop to try an access a
specific item of data. The code I showed was just an example from one
iteration of the loop.

> (I'm assuming this is what you mean when you say column).
>
> Here are the
> docs https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#for
>
>  If you want to access the column of data, assuming it's referenced from a
> model, just the attribute of that model. If you passed in a list to the
> template, you just use the django "dot" look-up syntax.

Perhaps my question wasn't clear. I have a list called headers. From
within a for loop I want the n'th item from the first row. In the loop
I am trying to access {{ headers.0.forloop.counter }} but I get no
value from that. But if I hard code the number (as a test), e.g.: {{
headers.0.2 }} then I do get the value.

Larry Martell

unread,
Mar 21, 2012, 10:58:09 AM3/21/12
to django...@googlegroups.com
On Wed, Mar 21, 2012 at 8:32 AM, Tom Evans <teva...@googlemail.com> wrote:
> On Wed, Mar 21, 2012 at 12:47 PM, Larry Martell <larry....@gmail.com> wrote:
>> This is probably a stupid newbie question ....
>>
>> I want to access a column of data in a row using forloop.counter, but
>> I cannot get it to work.
>>
>> In my test code, if I display {{ forloop.counter }} I get 2
>> If I display {{ headers.0.2 }} I get ToolType
>> But if I display {{ headers.0.forloop.counter }} I get nothing
>>
>> What is the proper syntax for this?
>>
>
> The preferred solution is to structure your data better in your view
> so that writing the template to render the data is straightforward.

This is a very large existing app, and I am new at this job. That have
a table of data and they want me to make some of the data in the table
into links that take them to a more detailed view of the data. The
functionality to provide this more detailed view of the data already
exist in the app. So I am just trying to construct the href. As I
iterate through the table data, I need info from the header to build
the href.

>
> The pragmatic solution is to use a custom filter to access your
> current data structures:
>
> http://stackoverflow.com/questions/2024660/django-sort-dict-in-template

OK, but is there some reason {{ headers.0.forloop.counter } does not
work when forloop.counter has a value of 2, yet {{ headers.0.2 }}
does work?

Tom Evans

unread,
Mar 21, 2012, 11:30:46 AM3/21/12
to django...@googlegroups.com
On Wed, Mar 21, 2012 at 2:58 PM, Larry Martell <larry....@gmail.com> wrote:
> OK, but is there some reason {{ headers.0.forloop.counter } does not
> work when forloop.counter has a value of 2, yet {{ headers.0.2 }}
> does work?

Django never does variable interpolation when resolving dot separated variables.

What django does do when it resolves dot separated variables is well documented:

https://docs.djangoproject.com/en/1.3/topics/templates/#variables

So outputting 'headers.0.forloop.counter' first finds the 'headers'
variable, and gets the 0th entry. In fact, first of all it tries

headers.get('0')

IE, a dictionary lookup. This fails, so it tries

getattr(headers, '0')

IE, an attribute lookup. This fails, so it tries

headers.0() *

IE, a method call. This fails, so it tries

headers[0]

IE, a list index lookup.

Having got 'headers.0' resolved, it would then step on to the next
part of the lookup - 'forloop'.

I think if you trace through yourself what it will do to lookup
forloop as a dictionary entry of headers[0], an attribute of
headers[0], a method call of headers[0] or a list index lookup of
headers[0], you will see that obviously all of these would fail. If
you replace forloop.counter with the raw value '2' and repeat the
process, you'll see why that works as well.

Cheers

Tom

* It doesn't actually do this, I haven't checked the code, but it
probably does something like this:

val = headers.get('0')
if not val:
val = getattr(headers, '0')
if callable(val):
return val()
if not val:
val = headers[0]
return val

James

unread,
Mar 21, 2012, 2:52:23 PM3/21/12
to django...@googlegroups.com


On Wednesday, March 21, 2012 10:41:08 AM UTC-4, Larry....@gmail.com wrote:
 With regards to your reply:


 Yes, I realize that. I am using it withing a loop to try an access a
specific item of data. The code I showed was just an example from one
iteration of the loop.

You can't use forloop and friends in the manner you attempting to use it. It's The reason is, headers.0 has no attribute named "forloop"

The best that you could do here, in this situation, is to create a custom filter and pass it forloop.counter as variable like so:
{{ headers.0|forloop.counter }} You will of course have to write that filter out first, register it, and install it in the template.

I'm still not sure if this is the right approach. Why not just iterate over the list itself, if you want to print the nth item within the list? Or, structure the list/queryset from the view such that you don't have to do such acrobatics.

 But if I hard code the number (as a test), e.g.:  {{
headers.0.2 }} then I do get the value. 

This works because headers.0 has an attribute that works by way of the django dot look up. See Tom Evan's post regarding this.

It seems to me that you have a list of lists (and not a single list). In which case, you can just iterate over the first list and iterate over the second list. If you need some control flow, just use the standard if / else conditions. But, maybe I'm missing something?

Even easier would be to use django's introspect feature and create models based on the tables in the DB, query the DB using the models and create querysets. Then you can access and display the data in a more coherent manner.



 

Larry Martell

unread,
Mar 21, 2012, 4:41:16 PM3/21/12
to django...@googlegroups.com

That would be really inefficient, as the table can have thousands of
rows, with 30 columns each. I'd have to iterate through the header row
for each column in each row to get the value to put into the href.

> Or,
> structure the list/queryset from the view such that you don't have to do
> such acrobatics.
>
>>  But if I hard code the number (as a test), e.g.:  {{
>> headers.0.2 }} then I do get the value.
>
>
> This works because headers.0 has an attribute that works by way of the
> django dot look up. See Tom Evan's post regarding this.
>
> It seems to me that you have a list of lists (and not a single list). In
> which case, you can just iterate over the first list and iterate over the
> second list. If you need some control flow, just use the standard if / else
> conditions. But, maybe I'm missing something?

Yes, I think you are. I have 2 header rows, and thousands of data
rows. For each column in each data row I have to construct a href. One
of the parameters in the href comes from the first header row. The
header rows get processed first, then the data rows (to build the
table). So by the time I am processing the data I don't have the
header any more. I should probably just do this in javascript.

> Even easier would be to use django's introspect feature and create models
> based on the tables in the DB, query the DB using the models and create
> querysets. Then you can access and display the data in a more
> coherent manner.
>
> see the docs here for
> introspection: https://docs.djangoproject.com/en/dev/howto/legacy-databases/?from=olddocs


That already is the case. I am trying to add functionality to an existing app.

Andre Terra

unread,
Mar 21, 2012, 9:12:39 PM3/21/12
to django...@googlegroups.com

You could always try writing a custom template tag or filter as someone else suggested earlier in the thread before resorting to JavaScript.

Cheers,
AT

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages