Hello,

I was given the following solution for storing a django object in a
javascript variable

//SOLUTION BEGINS HERE
1. Use a loop in the template:

var map_schools = [{% for school in city.school_set.all %}"{{
school|escapejs }}"{% if not forloop.last %},{% endif %}{% endfor %}];

You'll notice a couple of things there:
  - there is a "for" loop there, to iterate over all of the schools in the
result set
  - the quotes are present in the template, to make sure that the school
names are strings in the JavaScript
  - there is a test after every string to see if a "," is needed as a
separator (some javascript implementations are not very forgiving if you put
a comma after the last element)
  - Every school name is passed through the "escapejs" filter, to avoid
breaking out of the string.

//SOLUTION ENDS

Now, I was wondering how I can store the {{ school|escapejs }} into a
javascript array, and use the data inside school as if it were an array
element, like map_schools[1], map_schools[3] etc

Thanks

regards,

JK


On Fri, Jun 10, 2011 at 12:21 PM, Ian Clelland <clell...@gmail.com> wrote:

>
>
> On Fri, Jun 10, 2011 at 7:48 AM, jay K. <jay.developer2...@gmail.com>wrote:
>
>>
>> Hello,
>>
>> I am not a django developer, but I have a background on html, css,
>> javascript, jquery and php.
>>
>> I was wondering if you can help me with a question regarding django,
>> since I'm working on a website built on django (which was not started
>> by me)
>>
>> I want to store a django object into a javascript array. So far I have
>> this code:
>>
>> In my template file:
>>
>> <script type="text/javascript" language="javascript" charset="utf-8">
>>
>> var map_schools = {{ city.school_set.all }};
>>
>> </script>
>>
>> Unfortunately the django object is not stored as desired, but instead
>> it gets stored like the following:
>>
>> var map_schools = [&lt;School: Oxford, Eckersley&gt;]
>>
>>
> [<School: Oxford, Eckersley>] is the printable representation of your query
> set (what you get when you call city.school_set.all()) -- what you need to
> do is build up a JavaScript literal that you can insert into the code. There
> are two fairly simple ways of doing this:
>
> 1. Use a loop in the template:
>
> var map_schools = [{% for school in city.school_set.all %}"{{
> school|escapejs }}"{% if not forloop.last %},{% endif %}{% endfor %}];
>
> You'll notice a couple of things there:
>   - there is a "for" loop there, to iterate over all of the schools in the
> result set
>   - the quotes are present in the template, to make sure that the school
> names are strings in the JavaScript
>   - there is a test after every string to see if a "," is needed as a
> separator (some javascript implementations are not very forgiving if you put
> a comma after the last element)
>   - Every school name is passed through the "escapejs" filter, to avoid
> breaking out of the string.
>
> 2. Construct a JSON string in python:
>
> In your view, do something like this:
>
>     from django.utils.simplejson import dumps
>     ...
>     school_list = dumps([school.name for school in city.school_set.all()])
>
> and then add school_list to the context variables that are passed to the
> template. Then in the template, you can just use {{ school_list }}, and it
> will be a properly formatted JavaScript array.
>
>
>
> --
> Regards,
> Ian Clelland
> <clell...@gmail.com>
>
>  --
> 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.

Reply via email to