On Wed, Mar 21, 2012 at 12:52 PM, James <[email protected]> wrote:
>
>
> On Wednesday, March 21, 2012 10:41:08 AM UTC-4, [email protected] wrote:
>>
>> On Wed, Mar 21, 2012 at 7:53 AM, James <> wrote:
>> >
>> >
>> > On Wednesday, March 21, 2012 8:47:23 AM UTC-4, [email protected]
>> > 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.
>
> 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?
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 received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.