0

I'm passing a dictionary from my Django view and want to access the dictionary in my code.

view code:

res.responses is a dictionary

def index(request):
    import pprint 
    pprint.pprint(res.responses)
    print 'type = ', type(res.responses)
    return render_to_response("deploy/index.html", {"responses":res.responses})

Javascript code:

$(document).ready(function(){
    //{% for each in responses%}
    //  console.log(Hi)
    //{% endfor %}

    var response = "{{responses}}"
    console.log(response)

I tried accessing the variable directly using for loop and also accessing the variable directly. Both throw me an error. Please provide some suggestion.

1 Answer 1

1

You can do both.

Option 1: Provide the script via a template that will send the code with the values. Will look ugly but work. Your javascript file or even the html must be parsed by the django template engine. They can't be static

Option 2: Provide a new view with a json response, that is accessed from your javascript code (ie: via JQuery)

Sign up to request clarification or add additional context in comments.

Comments

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.