On Tuesday, 4 August 2015 17:32:11 UTC+1, Alex Heyden wrote:
>
> I don't quite follow what you're doing with set_first_call_true and 
> set_first_call_false. Are you using that to control the for loop behavior?
>
> If you are, take a look at 
> https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for. 
> Django's template language has that built in.
>
> If you're pushing to an array like in your initial example, you don't need 
> to handle commas at all. Here's an example out of Chrome's dev tools:
>
> > var a = []
> > a.push('foo')
> > a.push('bar')
> > a
> < ["foo", "bar"]
>
> If you want to define it in one go, try something more like:
>
> <script type="text/javascript">
>     my_markers = [
>     {% for current_addr in mymodel.addresses.all %}
>         {
>             lat: {{ current_addr.lat }},
>             lon: {{ current_addr.lon }},
>             description: {{ current_addr.description | safe }}
>         }{% if not forloop.last %}, {% endif %}
>     {% endfor %}
>     ];
> </script>
>
>
An even better solution is to keep this logic where it belongs, in the 
view, and use the accepted format for interchange of data with Javascript, 
which is JSON.

    my_markers = [{'lat': addr.lat, 'lon': addr.lon, 'description': 
addr.description} for addr in mymodel.addresses.all]
    return render(request, 'template.html', {'my_markers': 
json.dumps(my_markers)})

and refer to it directly in the template:

    <script type="text/javascript">
         my_markers = {{ my_markers|safe }}
    </script>

-- 
DR.

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4c82b618-008e-450f-9f64-5267ec795795%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to