Re: forloop.counter

2020-10-15 Thread GBELE CEDRIC EMMANUEL
{% if forloop.counter < variable  %}

Le jeu. 15 oct. 2020 à 15:01, nirali sanghvi <18comp.niralisang...@gmail.com>
a écrit :

> Ig this is bcoz u are using jinga format in a jinga format.
>
> On Thu 15 Oct, 2020, 8:04 PM luca72.b...@gmail.com, <
> luca72.bertolo...@gmail.com> wrote:
>
>> i have this :
>>  {% if forloop.counter < {{ variable }} %}
>> i have this error:
>>
>> Could not parse the remainder: '{{' from '{{'
>>
>> how i can solve it?
>> Thanks
>>
>> --
>> 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 django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/58585ced-68c2-4b46-862f-36f919d13555n%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-users/58585ced-68c2-4b46-862f-36f919d13555n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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 django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CANqtQVOsVi6uDpurd2AyDC6zyLJk%3D-wUX%3DC12ay%2B3nFs1S58MA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CANqtQVOsVi6uDpurd2AyDC6zyLJk%3D-wUX%3DC12ay%2B3nFs1S58MA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABTw%3DMVsxCySxHsmiHo04z_PH_thOvPgLnJAv42UZ5hNahODhQ%40mail.gmail.com.


Re: forloop.counter

2020-10-15 Thread nirali sanghvi
Ig this is bcoz u are using jinga format in a jinga format.

On Thu 15 Oct, 2020, 8:04 PM luca72.b...@gmail.com, <
luca72.bertolo...@gmail.com> wrote:

> i have this :
>  {% if forloop.counter < {{ variable }} %}
> i have this error:
>
> Could not parse the remainder: '{{' from '{{'
>
> how i can solve it?
> Thanks
>
> --
> 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 django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/58585ced-68c2-4b46-862f-36f919d13555n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/58585ced-68c2-4b46-862f-36f919d13555n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CANqtQVOsVi6uDpurd2AyDC6zyLJk%3D-wUX%3DC12ay%2B3nFs1S58MA%40mail.gmail.com.


Re: forloop.counter

2020-10-15 Thread luca72.b...@gmail.com
thanks i forgot it

Il giorno giovedì 15 ottobre 2020 alle 16:39:45 UTC+2 
larry.mart...@gmail.com ha scritto:

> On Thu, Oct 15, 2020 at 7:32 AM luca72.b...@gmail.com
>  wrote:
> >
> > i have this :
> > {% if forloop.counter < {{ variable }} %}
> > i have this error:
> >
> > Could not parse the remainder: '{{' from '{{'
> >
> > how i can solve it?
>
> You don't need {{ }} inside of a {% %}
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/92123136-638f-47c7-a49f-4ce97f34a4b9n%40googlegroups.com.


Re: forloop.counter

2020-10-15 Thread Larry Martell
On Thu, Oct 15, 2020 at 7:32 AM luca72.b...@gmail.com
 wrote:
>
> i have this :
>  {% if forloop.counter < {{ variable }} %}
> i have this error:
>
> Could not parse the remainder: '{{' from '{{'
>
> how i can solve it?

You don't need {{ }} inside of a {% %}

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACwCsY6NE7muV0oSnpUgAVA0sSfQnmp_i_cSb5DddwMGxbKe0Q%40mail.gmail.com.


forloop.counter

2020-10-15 Thread luca72.b...@gmail.com
i have this :
 {% if forloop.counter < {{ variable }} %}
i have this error:

Could not parse the remainder: '{{' from '{{' 

how i can solve it?
Thanks 

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/58585ced-68c2-4b46-862f-36f919d13555n%40googlegroups.com.


Re: using forloop.counter to access data

2012-03-21 Thread Andre Terra
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
On Mar 21, 2012 5:41 PM, "Larry Martell"  wrote:

> On Wed, Mar 21, 2012 at 12:52 PM, James  wrote:
> >
> >
> > On Wednesday, March 21, 2012 10:41:08 AM UTC-4, Larry@gmail.comwrote:
> >>
> >> On Wed, Mar 21, 2012 at 7:53 AM, James <> 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.
> >
> >  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 a

Re: using forloop.counter to access data

2012-03-21 Thread Larry Martell
On Wed, Mar 21, 2012 at 12:52 PM, James  wrote:
>
>
> On Wednesday, March 21, 2012 10:41:08 AM UTC-4, larry@gmail.com wrote:
>>
>> On Wed, Mar 21, 2012 at 7:53 AM, James <> 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.
>
>  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 django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: using forloop.counter to access data

2012-03-21 Thread James


On Wednesday, March 21, 2012 10:41:08 AM UTC-4, larry@gmail.com wrote:
>
> On Wed, Mar 21, 2012 at 7:53 AM, James <> wrote:
> >
> >
> > On Wednesday, March 21, 2012 8:47:23 AM UTC-4, Larry@gmail.comwrote:
> >>
> >> 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? 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.

see the docs here for introspection: 
https://docs.djangoproject.com/en/dev/howto/legacy-databases/?from=olddocs


 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/1F5cm7oGKOMJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: using forloop.counter to access data

2012-03-21 Thread Tom Evans
On Wed, Mar 21, 2012 at 2:58 PM, Larry Martell  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

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: using forloop.counter to access data

2012-03-21 Thread Larry Martell
On Wed, Mar 21, 2012 at 8:32 AM, Tom Evans  wrote:
> On Wed, Mar 21, 2012 at 12:47 PM, Larry Martell  
> 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?

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: using forloop.counter to access data

2012-03-21 Thread Larry Martell
On Wed, Mar 21, 2012 at 7:53 AM, James  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.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: using forloop.counter to access data

2012-03-21 Thread Tom Evans
On Wed, Mar 21, 2012 at 12:47 PM, Larry Martell  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.

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

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: using forloop.counter to access data

2012-03-21 Thread James


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 
(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.

Docs here: https://docs.djangoproject.com/en/dev/ref/templates/api/#render

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.

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



using forloop.counter to access data

2012-03-21 Thread Larry Martell
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?

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: How do I render a template variable inside a for loop, when the var is a dict and the key is forloop.counter

2011-09-09 Thread Andre Terra
I get it now.. Then go with the hackier solution I proposed!

On Thu, Sep 8, 2011 at 6:18 PM, Rami  wrote:

> Thanks.
> But note that I'm not looking for the dict value in MyList, rather: I
> loop on MyList and (trying to) retrieve the dict value from (another
> var) BOOK_PHONE with key=loop.counter.
> I think that makes it a bit more challenging, but still it's a fairly
> simple case. It's this limitation in the template syntax ... that
> doesn't allow you to do MyDict[key], instead you need to write
> MyDict.key (dot). But my key is "loop.counter", and
> MyDict.loop.counter (2 dots) is too much for the template to be
> interpreted correctly.
>
>
> On Sep 8, 12:51 pm, Andre Terra  wrote:
> > There is an inflexibility indeed, because the template is meant for
> > designers, not programmers.
> >
> > Try {% for key, value in MyList.items %} instead.
> >
> > A hackier solution would be to write your custom template tag like {%
> > get_from_dict foo bar %} to try to get foo[bar]. But in this case you can
> > just use items(), as it's often the case when iterating over
> dictionaries.
> >
> > Cheers,
> > AT
> >
> >
> >
> >
> >
> >
> >
> > On Thu, Sep 8, 2011 at 4:24 PM, Rami  wrote:
> > > Hi,
> > > I have a dict var defined in server side, like PHONE_BOOK={1:'634534',
> > > 2:'264886', 3:'455346' etc..}
> > > Then in my django template I have this loop where I need to print the
> > > PHONE_BOOK[loop counter]:
> > > {% for Item in MyList %}
> >
> > >  Item.first_name |  PHONE_BOOK.forloop.counter
> >
> > > {% endfor %}
> > > -
> > > For some reason PHONE_BOOK.forloop.counter doesn't work and neither
> > > the more direct ways like PHONE_BOOK[forloop.counter]; it seems to me
> > > that there's a bit of an inflexibility in the django template renderin
> > > mechanism.
> >
> > > Any idea how to solve that?
> >
> > > Thanks.
> >
> > > --
> > > 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+unsubscr...@googlegroups.com.
> > > For more options, visit this group at
> > >http://groups.google.com/group/django-users?hl=en.
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: How do I render a template variable inside a for loop, when the var is a dict and the key is forloop.counter

2011-09-08 Thread Bill Freeman
You might try a with tag to get the loop counter into a variable.

On Thu, Sep 8, 2011 at 5:18 PM, Rami  wrote:
> Thanks.
> But note that I'm not looking for the dict value in MyList, rather: I
> loop on MyList and (trying to) retrieve the dict value from (another
> var) BOOK_PHONE with key=loop.counter.
> I think that makes it a bit more challenging, but still it's a fairly
> simple case. It's this limitation in the template syntax ... that
> doesn't allow you to do MyDict[key], instead you need to write
> MyDict.key (dot). But my key is "loop.counter", and
> MyDict.loop.counter (2 dots) is too much for the template to be
> interpreted correctly.
>
>
> On Sep 8, 12:51 pm, Andre Terra  wrote:
>> There is an inflexibility indeed, because the template is meant for
>> designers, not programmers.
>>
>> Try {% for key, value in MyList.items %} instead.
>>
>> A hackier solution would be to write your custom template tag like {%
>> get_from_dict foo bar %} to try to get foo[bar]. But in this case you can
>> just use items(), as it's often the case when iterating over dictionaries.
>>
>> Cheers,
>> AT
>>
>>
>>
>>
>>
>>
>>
>> On Thu, Sep 8, 2011 at 4:24 PM, Rami  wrote:
>> > Hi,
>> > I have a dict var defined in server side, like PHONE_BOOK={1:'634534',
>> > 2:'264886', 3:'455346' etc..}
>> > Then in my django template I have this loop where I need to print the
>> > PHONE_BOOK[loop counter]:
>> > {% for Item in MyList %}
>>
>> >          Item.first_name |  PHONE_BOOK.forloop.counter
>>
>> > {% endfor %}
>> > -
>> > For some reason PHONE_BOOK.forloop.counter doesn't work and neither
>> > the more direct ways like PHONE_BOOK[forloop.counter]; it seems to me
>> > that there's a bit of an inflexibility in the django template renderin
>> > mechanism.
>>
>> > Any idea how to solve that?
>>
>> > Thanks.
>>
>> > --
>> > 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+unsubscr...@googlegroups.com.
>> > For more options, visit this group at
>> >http://groups.google.com/group/django-users?hl=en.
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: How do I render a template variable inside a for loop, when the var is a dict and the key is forloop.counter

2011-09-08 Thread Rami
Thanks.
But note that I'm not looking for the dict value in MyList, rather: I
loop on MyList and (trying to) retrieve the dict value from (another
var) BOOK_PHONE with key=loop.counter.
I think that makes it a bit more challenging, but still it's a fairly
simple case. It's this limitation in the template syntax ... that
doesn't allow you to do MyDict[key], instead you need to write
MyDict.key (dot). But my key is "loop.counter", and
MyDict.loop.counter (2 dots) is too much for the template to be
interpreted correctly.


On Sep 8, 12:51 pm, Andre Terra  wrote:
> There is an inflexibility indeed, because the template is meant for
> designers, not programmers.
>
> Try {% for key, value in MyList.items %} instead.
>
> A hackier solution would be to write your custom template tag like {%
> get_from_dict foo bar %} to try to get foo[bar]. But in this case you can
> just use items(), as it's often the case when iterating over dictionaries.
>
> Cheers,
> AT
>
>
>
>
>
>
>
> On Thu, Sep 8, 2011 at 4:24 PM, Rami  wrote:
> > Hi,
> > I have a dict var defined in server side, like PHONE_BOOK={1:'634534',
> > 2:'264886', 3:'455346' etc..}
> > Then in my django template I have this loop where I need to print the
> > PHONE_BOOK[loop counter]:
> > {% for Item in MyList %}
>
> >          Item.first_name |  PHONE_BOOK.forloop.counter
>
> > {% endfor %}
> > -
> > For some reason PHONE_BOOK.forloop.counter doesn't work and neither
> > the more direct ways like PHONE_BOOK[forloop.counter]; it seems to me
> > that there's a bit of an inflexibility in the django template renderin
> > mechanism.
>
> > Any idea how to solve that?
>
> > Thanks.
>
> > --
> > 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+unsubscr...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: How do I render a template variable inside a for loop, when the var is a dict and the key is forloop.counter

2011-09-08 Thread Andre Terra
There is an inflexibility indeed, because the template is meant for
designers, not programmers.

Try {% for key, value in MyList.items %} instead.

A hackier solution would be to write your custom template tag like {%
get_from_dict foo bar %} to try to get foo[bar]. But in this case you can
just use items(), as it's often the case when iterating over dictionaries.


Cheers,
AT

On Thu, Sep 8, 2011 at 4:24 PM, Rami  wrote:

> Hi,
> I have a dict var defined in server side, like PHONE_BOOK={1:'634534',
> 2:'264886', 3:'455346' etc..}
> Then in my django template I have this loop where I need to print the
> PHONE_BOOK[loop counter]:
> {% for Item in MyList %}
>
>  Item.first_name |  PHONE_BOOK.forloop.counter
>
> {% endfor %}
> -
> For some reason PHONE_BOOK.forloop.counter doesn't work and neither
> the more direct ways like PHONE_BOOK[forloop.counter]; it seems to me
> that there's a bit of an inflexibility in the django template renderin
> mechanism.
>
> Any idea how to solve that?
>
> Thanks.
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



How do I render a template variable inside a for loop, when the var is a dict and the key is forloop.counter

2011-09-08 Thread Rami
Hi,
I have a dict var defined in server side, like PHONE_BOOK={1:'634534',
2:'264886', 3:'455346' etc..}
Then in my django template I have this loop where I need to print the
PHONE_BOOK[loop counter]:
{% for Item in MyList %}

  Item.first_name |  PHONE_BOOK.forloop.counter

{% endfor %}
-
For some reason PHONE_BOOK.forloop.counter doesn't work and neither
the more direct ways like PHONE_BOOK[forloop.counter]; it seems to me
that there's a bit of an inflexibility in the django template renderin
mechanism.

Any idea how to solve that?

Thanks.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: if forloop.counter

2009-08-13 Thread When ideas fail

Thanks!

On 13 Aug, 20:52, Jonas Obrist  wrote:
> When ideas fail wrote:
> > Hello, i know you can have things like {% if forloop.last %}
> > but is it possible to have an if statement that matches the value of a
> > forloop counter
> > so it would be like:
>
> > {% if forloop.counter = 6 % }
>
> > or similar and how could i do that?
>
> > Thanks
>
> {% ifequal forloop.counter 6 %}
> ...
> {% endif %}
>
> Jonas
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: if forloop.counter

2009-08-13 Thread Jonas Obrist

When ideas fail wrote:
> Hello, i know you can have things like {% if forloop.last %}
> but is it possible to have an if statement that matches the value of a
> forloop counter
> so it would be like:
>
> {% if forloop.counter = 6 % }
>
> or similar and how could i do that?
>
> Thanks
>
> >
>   
{% ifequal forloop.counter 6 %}
...
{% endif %}

Jonas

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



if forloop.counter

2009-08-13 Thread When ideas fail

Hello, i know you can have things like {% if forloop.last %}
but is it possible to have an if statement that matches the value of a
forloop counter
so it would be like:

{% if forloop.counter = 6 % }

or similar and how could i do that?

Thanks

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to access an element in a list using forloop.counter in django templates?

2009-03-09 Thread Rama Vadakattu

Thanks for the clarification.

On Mar 10, 10:05 am, Malcolm Tredinnick 
wrote:
> On Mon, 2009-03-09 at 21:47 -0700, Rama Vadakattu wrote:
> > understood.
>
> > But Malcom,
>
> > 1.i feel doing restructuring of data in views for this purpose is not
> > appropriate.
>
> Then you are free to write a template filter to do what you like. It's
> probably about three lines long.
>
> > 2.And also It would be better if we can have tag / filter  which
> > directly gives us an element from the list by taking position as its
> > input.
>
> Fortunately, Django allows you to write one. In fact, writing filters to
> do things like this is encouraged. It's a feature.
>
> >    such as..   {{ list|forloop.counter }}. May be in django
> > next release.
>
> Pretty much no chance, I suspect. Indirect variable lookups are pretty
> much "programming in templates". The idea, if you're wanting to use the
> templates without additions, is to structure the data in the view
> function so that the template just dumps data into holes in the
> structure it provides. If you're having to compute which piece of data
> to get based on some other piece of data, then you can do that in this
> great language we've provided called "Python".
>
> That might sound arbitrary and I'm partially writing tongue-in-cheek,
> but the principle is very sound. Once you start having arbitrary
> language constructs in templates, the line between what logic to put in
> the template and what to keep in the Python function that produces the
> data becomes blurry and very inconsistent. Django's default setup
> strives to keep that line pretty clean.
>
> Some people don't like where the line is, which is fine. There's no
> satisfying everybody and it's one of the great reasons to permit and
> encourage custom template tags and filters and sharing thereof. The
> ability to make extensions is a first-class feature of Django's
> templates, not some accidental side-effect. And if that still doesn't
> satisfy you, you can also use pretty much any other templating language
> you like (even PHP) to render the results. I'd still encourage thinking
> about how to keep that kind of logic out of the template, having
> maintained some pretty large collections of templates in Turing-complete
> templating languages that steadily became harder and harder to control,
> but it's up to you which trade-offs you want to make as far as balancing
> "the now" and "the future".
>
> Regards,
> Malcolm
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to access an element in a list using forloop.counter in django templates?

2009-03-09 Thread Malcolm Tredinnick

On Mon, 2009-03-09 at 21:47 -0700, Rama Vadakattu wrote:
> understood.
> 
> But Malcom,
> 
> 1.i feel doing restructuring of data in views for this purpose is not
> appropriate.

Then you are free to write a template filter to do what you like. It's
probably about three lines long.

> 2.And also It would be better if we can have tag / filter  which
> directly gives us an element from the list by taking position as its
> input.

Fortunately, Django allows you to write one. In fact, writing filters to
do things like this is encouraged. It's a feature.

>such as..   {{ list|forloop.counter }}. May be in django
> next release.

Pretty much no chance, I suspect. Indirect variable lookups are pretty
much "programming in templates". The idea, if you're wanting to use the
templates without additions, is to structure the data in the view
function so that the template just dumps data into holes in the
structure it provides. If you're having to compute which piece of data
to get based on some other piece of data, then you can do that in this
great language we've provided called "Python".

That might sound arbitrary and I'm partially writing tongue-in-cheek,
but the principle is very sound. Once you start having arbitrary
language constructs in templates, the line between what logic to put in
the template and what to keep in the Python function that produces the
data becomes blurry and very inconsistent. Django's default setup
strives to keep that line pretty clean.

Some people don't like where the line is, which is fine. There's no
satisfying everybody and it's one of the great reasons to permit and
encourage custom template tags and filters and sharing thereof. The
ability to make extensions is a first-class feature of Django's
templates, not some accidental side-effect. And if that still doesn't
satisfy you, you can also use pretty much any other templating language
you like (even PHP) to render the results. I'd still encourage thinking
about how to keep that kind of logic out of the template, having
maintained some pretty large collections of templates in Turing-complete
templating languages that steadily became harder and harder to control,
but it's up to you which trade-offs you want to make as far as balancing
"the now" and "the future".

Regards,
Malcolm


--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to access an element in a list using forloop.counter in django templates?

2009-03-09 Thread Rama Vadakattu

understood.

But Malcom,

1.i feel doing restructuring of data in views for this purpose is not
appropriate.
In above problem it is obvious like
   1st iteration -1st element in list
.
.
.

  there may be cases where accessing a list may be complex and is not
so abvious.


2.And also It would be better if we can have tag / filter  which
directly gives us an element from the list by taking position as its
input.
   such as..   {{ list|forloop.counter }}. May be in django
next release.











On Mar 10, 9:30 am, Malcolm Tredinnick 
wrote:
> On Mon, 2009-03-09 at 21:21 -0700, Rama Vadakattu wrote:
> > I have a list in template.
> > I need to access that list based on a forloop.counter.
>
> > For example :
>
> > if forloop.counter is 1 then i need to access list[1]
> > if forloop.counter is 5 then i need to access list[5]
> > .
> > so on..
>
> Django's templates don't support indirect variable lookup like this. You
> need to either write a template filter to do the lookup based on the
> argument you pass in (forloop.counter), or find one on the internet --
> it might well already exist, but I have no idea because I arrange my
> template output to never require it.
>
> Alternatively restructure your data (in your view) so that you can
> iterate over the things you need all at once. For example, if list1 and
> list2 are the objects you're iterating over, change the view to pass
> zip(list1, list2) into the template and then iterate over the combined
> result with:
>
>         {% for val1,val2 in combined_result %}
>            ...
>         {% endfor %}
>
> Regards,
> Malcolm
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to access an element in a list using forloop.counter in django templates?

2009-03-09 Thread Malcolm Tredinnick

On Mon, 2009-03-09 at 21:21 -0700, Rama Vadakattu wrote:
> I have a list in template.
> I need to access that list based on a forloop.counter.
> 
> For example :
> 
> if forloop.counter is 1 then i need to access list[1]
> if forloop.counter is 5 then i need to access list[5]
> .
> so on..

Django's templates don't support indirect variable lookup like this. You
need to either write a template filter to do the lookup based on the
argument you pass in (forloop.counter), or find one on the internet --
it might well already exist, but I have no idea because I arrange my
template output to never require it.

Alternatively restructure your data (in your view) so that you can
iterate over the things you need all at once. For example, if list1 and
list2 are the objects you're iterating over, change the view to pass
zip(list1, list2) into the template and then iterate over the combined
result with:

{% for val1,val2 in combined_result %}
   ...
{% endfor %}

Regards,
Malcolm



--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to access an element in a list using forloop.counter in django templates?

2009-03-09 Thread Alex Gaynor
On Mon, Mar 9, 2009 at 11:21 PM, Rama Vadakattu wrote:

>
> I have a list in template.
> I need to access that list based on a forloop.counter.
>
> For example :
>
> if forloop.counter is 1 then i need to access list[1]
> if forloop.counter is 5 then i need to access list[5]
> .
> so on..
>
> I tried below techniques to solve the above problem.but none of them
> is working.
> Any hint on how to do this?
>
> 1. {%  for  k in klist  %}
>
>{{ mylist.forloop.counter }}  #This code is not
> working
>{% endfor %}
>
>
> 2.  {%  for  k in klist  %}
>
>   {{% with forloop.counter as xl %}
>{{mylist.x }} #This code is not working
>  {% endwith %}
>
>   { %endfor %}
>
> 3.  {%  for  k in klist  %}
> {{  mylist|slice:forloop.counter:forloop.counter }} #This
> code is not working
>   { %endfor %}
>
> Regards
> Rama vadakattu
>
>
>
>
>
>
>
>
>
>
> >
>
is there a reason you can't just do {{ k }} why would an item be at a
different position then it's index?

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



How to access an element in a list using forloop.counter in django templates?

2009-03-09 Thread Rama Vadakattu

I have a list in template.
I need to access that list based on a forloop.counter.

For example :

if forloop.counter is 1 then i need to access list[1]
if forloop.counter is 5 then i need to access list[5]
.
so on..

I tried below techniques to solve the above problem.but none of them
is working.
Any hint on how to do this?

1. {%  for  k in klist  %}

{{ mylist.forloop.counter }}  #This code is not
working
{% endfor %}


2.  {%  for  k in klist  %}

   {{% with forloop.counter as xl %}
{{mylist.x }} #This code is not working
  {% endwith %}

   { %endfor %}

3.  {%  for  k in klist  %}
 {{  mylist|slice:forloop.counter:forloop.counter }} #This
code is not working
   { %endfor %}

Regards
Rama vadakattu










--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to set FORLOOP.COUNTER=0?

2007-05-16 Thread [EMAIL PROTECTED]

Thanks James.

On May 16, 12:13 pm, "James Bennett" <[EMAIL PROTECTED]> wrote:
> On 5/16/07, Renato Lipi <[EMAIL PROTECTED]> wrote:
>
> > I am using FORLOOP built-in tag. There is a time that I need to restart this
> > count, setting forloop.counter=0. How can I do something like that?
>
> The plain template language (deliberately) cannot perform variable
> assignments; custom template tags can, though, so you might look into
> writing a tag to "reset" the loop.
>
> You might also consider whether there's some logic missing in your
> view which would make this template loop reset unnecessary.
>
> --
> "Bureaucrat Conrad, you are technically correct -- the best kind of correct."


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to set FORLOOP.COUNTER=0?

2007-05-16 Thread James Bennett

On 5/16/07, Renato Lipi <[EMAIL PROTECTED]> wrote:
> I am using FORLOOP built-in tag. There is a time that I need to restart this
> count, setting forloop.counter=0. How can I do something like that?

The plain template language (deliberately) cannot perform variable
assignments; custom template tags can, though, so you might look into
writing a tag to "reset" the loop.

You might also consider whether there's some logic missing in your
view which would make this template loop reset unnecessary.

-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to set FORLOOP.COUNTER=0?

2007-05-16 Thread [EMAIL PROTECTED]

Ok then...

So, actually I'm "plotting" tables with only 1 row and 15 columns,
table below table. Often, when I change the subject I working with,
there's a row that has less then 15 columns. Let's say the last line
(table) had 2 columns. If the next line is in the "for" that created
last line, the new one will contain 13 columns (and it should have
15).

Was it clear? Sorry about my English. I'll try to improve it next
time.

Nice to meet you Malcolm! I read some reply you sent to a friend of
mine.
Regards,
Renato.

On May 16, 11:04 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Wed, 2007-05-16 at 10:57 -0300, Renato Lipi wrote:
> > Hi there,
>
> > I am using FORLOOP built-in tag. There is a time that I need to
> > restart this count, setting forloop.counter=0. How can I do something
> > like that?
>
> You can't do this with the existing "for" tag. It crosses the line into
> trying to program with templates.
>
> Maybe if you describe the problem you're trying to solve, we can suggest
> a different approach.
>
> Regards,
> Malcolm


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to set FORLOOP.COUNTER=0?

2007-05-16 Thread Malcolm Tredinnick

On Wed, 2007-05-16 at 10:57 -0300, Renato Lipi wrote:
> Hi there,
> 
> I am using FORLOOP built-in tag. There is a time that I need to
> restart this count, setting forloop.counter=0. How can I do something
> like that?

You can't do this with the existing "for" tag. It crosses the line into
trying to program with templates.

Maybe if you describe the problem you're trying to solve, we can suggest
a different approach.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



How to set FORLOOP.COUNTER=0?

2007-05-16 Thread Renato Lipi
Hi there,

I am using FORLOOP built-in tag. There is a time that I need to restart this
count, setting forloop.counter=0. How can I do something like that?

Thanks.
Renato.

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---