For loop problem in template..Is this correct

1,886 views
Skip to first unread message

Adam Zedan

unread,
Aug 15, 2011, 7:45:07 PM8/15/11
to django...@googlegroups.com
Hi i am getting a problem with my for loop which i used in my template.Could you kindly let me know what is going wrong in here.
The for loop is in a jquery function

$(function() {
            var data = [];
             
               {% for x in range(len(content)) %}
                data[i] =
                {
                    roll_no: {{content[x].roll_no}},
                    cell_no: {{content[x].cell_no}},
                    nationality:{{content[x].nationality}},
                    e_mail:{{content[x].e_mail}}
                   
                };
                {% endfor %}
               
           
            grid = new Slick.Grid("#myGrid", data, columns, options);

            $("#myGrid").show();
        })

Konstantin Sushenko

unread,
Aug 15, 2011, 7:50:04 PM8/15/11
to Django users
hello,

as a result of this you will have a series of 'data[i] = ...'
assignments in your output. where 'i' would be undefined.

konstantin

Adam Zedan

unread,
Aug 15, 2011, 7:54:54 PM8/15/11
to django...@googlegroups.com
Ooops.. sorry for the i:

The code is

$(function() {
            var data = [];
                {% for x in range(len(content)) %}
                data[x] =
                {
                    roll_no: {{content[x].roll_no}},
                    cell_no: {{content[x].cell_no}},
                    nationality:{{content[x].nationality}},
                    e_mail:{{content[x].e_mail}}
                   
                };
                {% endfor %}
            grid = new Slick.Grid("#myGrid", data, columns, options);
            $("#myGrid").show();
        })

and the error i get is

Exception Type:     TemplateSyntaxError
Exception Value:     Could not parse the remainder: '(len(content))' from 'range(len(content))'


Template error

In template d:\django-1.3\django\bin\onlinedemo\dbdemo\templates\db.html, error at line 34
Could not parse the remainder: '(len(content))' from 'range(len(content))'

--------------


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


Landy Chapman

unread,
Aug 15, 2011, 7:58:23 PM8/15/11
to Django users


On Aug 15, 11:45 pm, Adam Zedan <zedan...@gmail.com> wrote:
> Hi i am getting a problem with my for loop which i used in my template.Could
> you kindly let me know what is going wrong in here.
> The for loop is in a jquery function

I think this:
>                {% for x in range(len(content)) %}

.. is not allowed --no python in django templates.

Do you have a sample (2 or 3 elements) showing how content is defined?
If content came from a query, this should work:

> {% for x in content %}
>                 data[i] =
>                 {
>                     roll_no: {{x.roll_no}},
>                     cell_no: {{x.cell_no}},
>                     nationality:{{x.nationality}},
>                     e_mail:{{x.e_mail}}
>                 };
>                 {% endfor %}

Adam Zedan

unread,
Aug 15, 2011, 8:05:28 PM8/15/11
to django...@googlegroups.com
okay so i temporarily removed the loop just to see if the template works

$(function() {
            var data = [];
              
                data[0] =
                {
                    roll_no: {{content[0].roll_no}},
                    cell_no: {{content[0].cell_no}},
                    nationality:{{content[0].nationality}},
                    e_mail:{{content[0].e_mail}}                   
                };

            grid = new Slick.Grid("#myGrid", data, columns, options);
            $("#myGrid").show();
        })


The final return in the view which calls the above templates is
return render_to_response("db.html",{'content':list})

and now i am getting the exception
Exception Value:    
Could not parse the remainder: '[0].roll_no' from 'content[0].roll_no'

any ideas???


--

Landy Chapman

unread,
Aug 15, 2011, 8:07:23 PM8/15/11
to Django users
@Konstantin Sushenko nice catch!
>> as a result of this you will have a series of 'data[i] = ...'
>>assignments in your output. where 'i' would be undefined.

So could he use this: JS isn't my thing, and this is approaching
too_clever
> $(function() {
i = 0;
>             var data = [];
>                 {% for x in content %}
>                 data[i] =
>                 {
>                     roll_no: {{x.roll_no}},
>                     cell_no: {{x.cell_no}},
>                     nationality:{{x.nationality}},
>                     e_mail:{{x.e_mail}}
>                 };
i +=1;

Adam Zedan

unread,
Aug 15, 2011, 8:22:02 PM8/15/11
to django...@googlegroups.com
I dont think we could even use "[]" as in

      roll_no: {{content[0].roll_no}},
am i correct  ?? couldnt find any reference to it on
https://docs.djangoproject.com/en/dev/topics/templates/

Landy Chapman

unread,
Aug 15, 2011, 8:38:07 PM8/15/11
to Django users
You may be right. I am out of my element (pardon the pun). However,
assuming your "content" was this:

content = [ { 'roll_no':1, 'cell_no': 1, 'nationality':'nation1'},
{ 'roll_no':2, 'cell_no': 2, 'nationality':'nation2'},
{ 'roll_no':3, 'cell_no': 3, 'nationality':'nation3'},

the django template renderer will turn this (note quotes around
nationality and email):
> > $(function() {
> i = 0;
> > var data = [];
> > {% for x in content %}
> > data[i] =
> > {
> > roll_no: {{x.roll_no}},
> > cell_no: {{x.cell_no}},
> > nationality: '{{x.nationality}}',
> > e_mail: '{{x.e_mail}}'
> > };
> i +=1;
> > {% endfor %}

into this:
$(function() {
i = 0;
var data = [];

data[i] =
{
roll_no: 1,
cell_no: 1,
nationality: 'nation 1',
e_mail: 'email1'
};
i +=1;
data[i] =
{
roll_no: 2,
cell_no: 2,
nationality: 'nation2',
e_mail: 'email2'
};
i +=1;
data[i] =
{
roll_no: 3,
cell_no: 3,
nationality: 'nation3',
e_mail: 'email3'
};
i +=1;

My JS guy has left for the day... maybe someone else can help out.
Also the 'autoescaping' should be on, in case some nationality
contains a (') Ex: "G'ermany"

HTH

Konstantin Sushenko

unread,
Aug 15, 2011, 8:48:50 PM8/15/11
to Django users
I would just write a template tag that outputs the whole JS function.
templates are too limiting in what you can use in {{}} brackets.

konstantin

victorkendy

unread,
Aug 16, 2011, 3:18:35 PM8/16/11
to Django users
Hi

The for tag iterates over the elements of the list you are using so
your code should be

$(function() {
var data = [];
var i = something();
{% for x in content %}
data[i] =
{
roll_no: {{x.roll_no}},
cell_no: {{x.cell_no}},
nationality:{{x.nationality}},
e_mail:{{x.e_mail}}
};
{% endfor %}

grid = new Slick.Grid("#myGrid", data, columns, options);

$("#myGrid").show();
})

Reply all
Reply to author
Forward
0 new messages