Reputation: 175
I am passing a python dictionary to a Django template html script as:-
def create_dict(request):
#creating the dictionary and storing it in py_dict
py_dict = json.dumps(py_dict)
context = {
"py_dict": py_dict
}
render(request, 'index.html', context)
In the Django template (index.html
), I am retrieving the dictionary as follows:-
<script language='javascript'>
$(document).ready(function(){
var dict = "{{py_dict}}";
console.log(dict);
});
</script>
If I try to assign the variable dict
directly the Django variable(py_dict
) without the double qoutes(var dict = {{py_dict}};
) then it shows Not found & error
The actual python dictionary looks like this:-
{"19.2.000/": {"19.2.000/dataconversion/": {}, "19.2.000/reim/": {}, "19.2.000/resa/": {}, "19.2.000/rms/": {}}, "19.2.001/": {"19.2.001/dataconversion/": {}}, "19.2/": {"19.2/alloc/": {}, "19.2/rpm/": {}}, "RESA/": {"RESA/folder1/": {}}, "RMS/": {}, "RPM/": {}, "/": {}, "apc/": {"apc/apc_inner/": {}}, "dataconversion/": {}, "folder2/": {}, "merch/": {"merch/19.0.x/": {"merch/19.0.x/19.0.000.1/": {"merch/19.0.x/19.0.000.1/dataconversion/": {}}, "merch/19.0.x/19.0.001/": {"merch/19.0.x/19.0.001/alloc/": {}, "merch/19.0.x/19.0.001/reim/": {}, "merch/19.0.x/19.0.001/resa/": {}, "merch/19.0.x/19.0.001/rms/": {"merch/19.0.x/19.0.001/rms/reports/": {}}, "merch/19.0.x/19.0.001/rpm/": {}}, "merch/19.0.x/19.0.002.1/": {"merch/19.0.x/19.0.002.1/Merged_RC/": {"merch/19.0.x/19.0.002.1/Merged_RC/rms/": {}}, "merch/19.0.x/19.0.002.1/hotfix/": {"merch/19.0.x/19.0.002.1/hotfix/rms/": {}}}, "merch/19.0.x/19.0.002/": {"merch/19.0.x/19.0.002/alloc/": {}, "merch/19.0.x/19.0.002/dataconversion/": {}, "merch/19.0.x/19.0.002/jos_merch/": {}, "merch/19.0.x/19.0.002/reim/": {}, "merch/19.0.x/19.0.002/resa/": {}, "merch/19.0.x/19.0.002/rms/": {"merch/19.0.x/19.0.002/rms/rms19installer.zip": {}}}}}}
but when I console.log
it in the script tag, the output is this:-
{"19.2.000/": {"19.2.000/dataconversion/": {}, "19.2.000/reim/": {}, "19.2.000/resa/": {}, "19.2.000/rms/": {}}, "19.2.001/": {"19.2.001/dataconversion/": {}}, "19.2/": {"19.2/alloc/": {}, "19.2/rpm/": {}}, "RESA/": {"RESA/folder1/": {}}, "RMS/": {}, "RPM/": {}, "/": {}, "apc/": {"apc/apc_inner/": {}}, "dataconversion/": {}, "folder2/": {}, "merch/": {"merch/19.0.x/": {"merch/19.0.x/19.0.000.1/": {"merch/19.0.x/19.0.000.1/dataconversion/": {}}, "merch/19.0.x/19.0.001/": {"merch/19.0.x/19.0.001/alloc/": {}, "merch/19.0.x/19.0.001/reim/": {}, "merch/19.0.x/19.0.001/resa/": {}, "merch/19.0.x/19.0.001/rms/": {"merch/19.0.x/19.0.001/rms/reports/": {}}, "merch/19.0.x/19.0.001/rpm/": {}}, "merch/19.0.x/19.0.002.1/": {"merch/19.0.x/19.0.002.1/Merged_RC/": {"merch/19.0.x/19.0.002.1/Merged_RC/rms/": {}}, "merch/19.0.x/19.0.002.1/hotfix/": {"merch/19.0.x/19.0.002.1/hotfix/rms/": {}}}, "merch/19.0.x/19.0.002/": {"merch/19.0.x/19.0.002/alloc/": {}, "merch/19.0.x/19.0.002/dataconversion/": {}, "merch/19.0.x/19.0.002/jos_merch/": {}, "merch/19.0.x/19.0.002/reim/": {}, "merch/19.0.x/19.0.002/resa/": {}, "merch/19.0.x/19.0.002/rms/": {"merch/19.0.x/19.0.002/rms/rms19installer.zip": {}}}}}}
in which "
is added for every "
and obviously it is not in the actual JSON format which javascript can process. And also since I am passing the Django variable (py_dict
) inside double quotes to the variable dict
that itself is also a problem since it becomes a string variable due to which I cannot perform any actions on the elements of the Django variable dictionary py_dict
How do I convert this into a proper javascript JSON format?
Upvotes: 2
Views: 4101
Reputation: 609
Instead of
var dict = "{{py_dict}}";
you can simply use:
var dict = {{ py_dict | safe }};
You can check the safe
template tag here https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#safe
Upvotes: 3
Reputation: 21807
As a rule of thumb never render anything from Django directly inside javascript. This can lead to potential XSS attacks. If you want data from the server either use AJAX if it is needed dynamically or you can use the json_script
template filter [Django docs] if it is needed only once when the page gets loaded:
Somewhere in the HTML:
{{ py_dict|json_script:"py_dict" }}
This will render like:
<script id="py_dict" type="application/json">JSON HERE</script>
Next in javascript you can simply use:
var dict = JSON.parse(document.getElementById('py_dict').textContent);
Upvotes: 10