我正在尝试在我的 javaScript 文件中从 django 服务器设置值
views.py
def convert(input):
if isinstance(input, dict):
return {convert(key): convert(value) for key, value in input.iteritems()}
elif isinstance(input, list):
return [convert(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
@csrf_exempt
def changebutton(request):
ids = request.GET['id']
t = get_template('Jquerymobilechangofdetailbutton222.html')
record = changeButton.objects(id = str(ids))[0]
dictionary = record.to_mongo()
if dictionary.has_key('_id'):
dictionary['ids'] = dictionary['_id']
del dictionary['_id']
dictionary = convert(dictionary)
print dictionary, "dict"
html = t.render(Context(dictionary))
return HttpResponse(html)
server log
{'username': 'Alok', '_types': ['changeButton'],
'ids': ObjectId('517e15f141d34226fc61525c'),
'_cls': 'changeButton',
'usergroup': ['user', 'admin'],
'details':[{'_types': ['userdetails'], 'add2': 'ee', 'add1': 'dd', 'mname': 'bb', 'lname': 'cc', '_cls': 'userdetails', 'fname': 'aa'}]} dict
HTML page
<script>
dict = {{usergroup}}
console.log(dict)
</script>
问题是它给出dict = ['user', 'admin']
和给出无效的语法(由于存在'
)任何帮助将不胜感激