Here is the deal:
You need to convert a Python array or dictionary to a JavaScript array or Hash(dictionary) through a Django template.
The solution is to use autoscaping on Django templates.
This would be the Django view wich passes the array or dictionary variable:
def javascript(request):
product1 = ['pendientes', 'pendiente pequeno', '10 euros' ]
product2 = { 'name': 'pendientes' , 'description' : 'Pendientes aro bronce', 'price' : '14.6 euro' }
return render_to_response('javascript.html', {'product1' : product1,'product2' : product2,})
This would be the Django template with the Javascript code:
{% extends "base.html" %}
{% block content %}
<script type="text/javascript">
{% autoescape off %}
var product1 = {{ product1 }} ;
var product2 = {{ product2 }} ;
{% endautoescape %}
document.write( 'product1 description: ', product1[1], '<br/>' );
document.write( 'product2 price: ' , product2["price"], '<br/>' );
</script>
{% endblock %}
Advertisement

