0

I have a django template variable {% clients_list %}
I want to load this in multiple select boxes with same prefixes.
I am writing this code:

$(document).ready(function(){
    for (i=1; i<=30; i++){
        for(j=0; j<=clients_list.length - 1; j++){
            $('#client'+i).append($('<option>')
            .text(clients_list[j])
            .attr('value', clients_list[j]));
            }
        }
    });

But I am facing this error:

ReferenceError: clients_list is not defined

Help me please!

1 Answer 1

2

As always, encode as JSON.

{% load jsonify %}

var clients_list = {{ clients_list|jsonify }};
Sign up to request clarification or add additional context in comments.

2 Comments

Just found that, this too works: var clients_list = {{ clients_list|safe }};
@MHS: Python's list representation is not the same as JSON.

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.