0

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 }}
6
  • What does {{ companies }} evaluate to? Javascript doesn't seem to like that syntax. Commented Apr 27, 2012 at 22:11
  • You should convert companies to JSON - only then JS would be able to read it Commented Apr 27, 2012 at 22:12
  • @Greg companies is the model object i get from db.. Commented Apr 27, 2012 at 22:13
  • @hamczu can you explain a bit more for me please? thank you! Commented Apr 27, 2012 at 22:14
  • looks like django syntax, you should probably specify that in your question and tags Commented Apr 27, 2012 at 22:15

1 Answer 1

2

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

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.