这适用于我的本地计算机,但是当我在我的VPS上运行时,它总是在这行中抛出一个javascript erorr:
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'}}];
在我的本地机器中(没有发现错误),差异是json值不显示' u'在他们面前签名。
这就是我将字典传递给上下文的方式:
return render(request, 'index.html', {'post_objs': SafeString(response)}, content_type='text/html')
这就是我渲染模板的方式:
var post_array = {{post_objs|safe}};
有什么想法吗?
答案 0 :(得分:3)
您需要使用json.dumps()
。像这样:
import json
return render(request, 'index.html', {'post_objs': json.dumps(response)}, content_type='text/html')