1

How would it be possible to access or iterate a django list within javascript using a changing INDEX.

For example:

<script>
    for (var i=0; i < "{{ django_list|length }}".toInt(); i++) {
         var e = "{{ django_list" + i.toString() + " }}"; // this gives an error
         ...    //  it does not parse a chunked ^ template variable properly
         ...
    }
</script>

Some background. This is a pure .js file that is being rendered by django. That part works, it just does not interpret a substringed out django template var syntax.

I'd prefer a solution which avoids encoding into JSON or anything similar. Template form is preferred because I need the methods of the variables.

Thanks.

2
  • Can u not use a for each loop? Commented Jul 1, 2013 at 21:04
  • Not for this example, JS is controlling the iteration Commented Jul 1, 2013 at 21:23

2 Answers 2

1

As always, encode as JSON.

{% load jsonify %}

var data = {{ django_list|jsonify }};

for (...) {
   ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

Ah, that's what my code looks like currently, but is there any way which avoids encoding? I need the models METHODS in the template, and encoding into JSON means everything aside from fields are lost. Thanks
You can't access the Django code from JavaScript without performing a XHR. If you don't actually need the code then create a list or dict from the data you do need and encode that.
You can call methods, which is good enough. {{ some_model.some_function }}. And encoding the model into JSON makes it so we lose this functionality.
"then create a list or dict from the data you do need and encode that" So you mean for every model call the method needed, create a list and pass it into the template? Can you be more clear on the comment.
0

Can't you use a for each loop?

{%  for item in django_list  %}
//Whatever you wish to do with item goes here
{% endfor %}

For more see the link https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for-empty

7 Comments

I just wanted to see if doing what was above in the example was possible. Good suggestion though
There are situations where we need the index. What if it was a big JS file with a global index which is changing dynamically. We can' just have a for loop in the JS, we need to use the index to access the list.
I do not understand you. As I said within the for loop you can always have the forloop.counter using which u can manipulate your items. Am I not getting something?
If you just had a for loop with the django {% %} syntax, it would iterate all at once when the loop is hit, independent of the outside JS. That's not the intended goal though. What if the index incrementing was based off of other factors? Like jQuery click event handlers.
Guess you are right you could always build your own template tag if your requirements are so particular.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.