Hi Im trying to execute python from javascript .So I'm trying to use the below code example
views.py
from django.shortcuts import render
from json import dumps
def send_dictionary(request):
# create data dictionary
dataDictionary = {
'hello': 'World',
'geeks': 'forgeeks',
'ABC': 123,
456: 'abc',
14000605: 1,
'list': ['geeks', 4, 'geeks'],
'dictionary': {'you': 'can', 'send': 'anything', 3: 1}
}
dataJSON = dumps(dataDictionary)
return render(request, 'main / landing.html', {'data': dataJSON})
landing.html
<!DOCTYPE html>
<body>
<div style="width: 40%;
border: 1px solid black;
background-color: lightcyan;
font-family: Helvetica, sans-serif;">
<div style="margin: 5%;">
<h2>
<u>Data</u>
</h2>
<h4 id='data'></h4>
</div>
</div>
</body>
</html>
<script>
var data = JSON.parse('{{data|escapejs}}')
var dataNode = document.getElementById('data');
for(var x in data){
dataNode.innerHTML+=x+' : '+data[x]+'<br><br>';
}
</script>
When I try to run ,I got this syntax error but doesn't really seem to be a syntax error.
Uncaught SyntaxError: Expected property name or '}' in JSON at position 1 at JSON.parse ()
Im referring to the example in the below link.
https://www.geeksforgeeks.org/how-to-pass-data-to-javascript-in-django-framework/