Reputation: 17806
I have an object "company". When I use company inside of html, it works fine. But when I tried to use that object inside javascript I get the "Uncaught SyntaxError: Unexpected token &".
What I am trying to do is getting the objects from db then display them on html page then change up some divs using js.
the url:
('^all_companies$', 'companies.views.all_companies')
the view:
def all_companies(request):
companies = Company.objects.all().order_by('id')[:5];
return direct_to_template(request, 'all_companies.html', {'companies': companies} );
the html:
{% block sidebar %}
<div id="sidebar">
<!-- like google maps, short list of company info -->
<ul>
{% for comp in companies %}
<li>{{ comp }}</li>
{% endfor %}
</ul>
</div>
{% endblock %}
the js:
var tmp = {{ companies }}
Upvotes: 0
Views: 525
Reputation: 14400
You can add a template filter like that one: http://djangosnippets.org/snippets/201/
and use
{{ companies | jsonify }}
but I'm not sure it's a good idea to do that directly on a db object, it will be better to map them to a simple map of properties you need
Upvotes: 2