【发布时间】:2014-12-29 09:04:59
【问题描述】:
我在 Django 中有以下键值对象:
data = {
"id": 1,
"question_text": "אנא בחר אחת מהתשובות הבאות",
"answers": [
{
"label" : "תשובה 1",
"value" : 1,
"count" : 30
},
{
"label" : "תשובה 2",
"value" : 2,
"count" : 30
},
{
"label" : "תשובה 3",
"value" : 3,
"count" : 30
},
}
请注意,有些数据是希伯来语的,所以当我将其保存到我使用的数据库中时:
unicode(self.answer_text).encode('utf-8')
当我尝试将此对象发送到视图时,以便在 Django 模板和 Javascript 中使用它
我用过这条线:
return render(request, 'reports/report.html', {'data': data })
在我使用的视图中:
var question_data = {{ data }} #in order to get the data object that was sent to the view
但我得到了这个元素:
{'bad': 45, 'good': 55, 'question_text': u'\u05e2\u05d3 \u05db\u05de\u05d4 \u05d0\u05ea\u05d4 \u05de\u05e8\u05d5\u05e6\u05d4 \u05d0\u05d5 \u05dc\u05d0 \u05de\u05e8\u05d5\u05e6\u05d4 \u05de\u05d1\u05d2\u05d3\u05d9 \u05e2\u05dc\u05d9\u05ea \u05d1\u05d0\u05d5\u05e4\u05df \u05db\u05dc\u05dc\u05d9?', 'id': u'8', 'answers': [{'value': 30, 'label': u'\u05de\u05d0\u05d5\u05d3 \u05de\u05e8\u05d5\u05e6\u05d4'}, {'value': 25, 'label': u'\u05d3\u05d9 \u05de\u05e8\u05d5\u05e6\u05d4'}, {'value': 20, 'label': u'\u05dc\u05d0 \u05db\u05dc \u05db\u05da \u05de\u05e8\u05d5\u05e6\u05d4'}, {'value': 25, 'label': u'\u05db\u05dc\u05dc \u05dc\u05d0 \u05de\u05e8\u05d5\u05e6\u05d4'}]}
控制台中出现这个错误:
SyntaxError: Unexpected token '&'. Expected a property name
我也试过用:
var question_data = {{ data|safe }}
我得到了这个错误:
[Error] SyntaxError: Unexpected string literal '\u05e2\u05d3 ...
我正在使用 Django 1.7 和 Python 2.7.6
请尝试帮助我理解我做错了什么
【问题讨论】:
标签: javascript python json django python-2.7