0

I wasn't able to find similar topic in google.

I return simple object into the django template through views

views:

return render(request, 'mainPage.html', {
    'obj': getObj()
})

def getObj():
    path = '/home/myPage' + 'obj.json'

    fd = open( path, 'r')
    obj = fd.read()
    fd.close()

    return json.dumps(obj)

template:

<input type="hidden" id="obj" data-obj="{{ obj }}">

and JS:

var obj = JSON.parse( $('#obj').data('obj') );
console.log( obj );

I see in the console the right result:

{
    "2018": {
        "First": {
            "obj1": "4",
            "obj2": "231",
        }
    }
}

but when I try to refer to this obj by console.log(obj ['2018']); it returns undefined

Will be thankful for your help Thanks in advance

1

1 Answer 1

0

It seems like obj.json is already a JSON file. There is no need to call json.dumps on the data after reading it. Just pass obj directly.

Also, you will need to disable auto escaping in the template by outputting it as {{ obj|safe }}.

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.