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.
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
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.
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?
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
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.
But if I hard code the number (as a test), e.g.: {{
headers.0.2 }} then I do get the value.
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.
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.