1

This is working on my local machine, but when I run it on my VPS it always throws a javascript erorr in this line:

var post_array = [{'title': u'How To Hire A Designer', 'url': u'/post/how-to-hire-a-designer', 'text': u'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ultricies lobortis eros, at tristique sem pulvinar non. Fusce nisi purus, lobortis non sagittis ac, venenatis eget ante. Nulla sem lacus, gravida sit amet sem sed, aliquet efficitur est. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec in sem sem. Nunc eros ex, pharetra ut enim vitae, ultrices mollis sapien. Donec eu consequat quam. Ut purus odio, venenatis pellentesque tellus vitae, iaculis bibendum urna. Vivamus vel velit at mi eleifend cursus a a velit. Nulla facilisi. Morbi sodales ex vel lectus dignissim, in volutpat erat pharetra. Vestibulum quis mi in metus mattis malesuada.Aenean et eleifend tortor. Nulla leo arcu, mattis in mattis nec, venenatis non sapien. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec dignissim iaculis fermentum. Suspendisse potenti. Duis rhoncus eros sed sapien mollis, at varius purus aliquet. Vivamus a quam nisi. Integer eu feli...', 'shares': 0, 'post_id': 1, 'comment_count': 15, 'date': {'year': 2014, 'day': 10, 'month': 'September'}}];

In my local machine (where no error is found) the diference is the json values don't show 'u' sign before them.

This is how I'm passing the dictionary to the context:

return render(request, 'index.html', {'post_objs': SafeString(response)}, content_type='text/html')

This is how I'm rendering the template:

var post_array = {{post_objs|safe}};

Any ideas?

1
  • JSON uses double quotes. Commented Oct 10, 2014 at 17:45

1 Answer 1

3

You need to use json.dumps(). Like this:

import json

return render(request, 'index.html', {'post_objs': json.dumps(response)}, content_type='text/html')
Sign up to request clarification or add additional context in comments.

1 Comment

Solved, another solution is using jsonify. pypi.python.org/pypi/django-jsonify/0.2

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.