1

我正在尝试在我的 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 = [&#39;user&#39;, &#39;admin&#39;]和给出无效的语法(由于存在&#39;)任何帮助将不胜感激

4

1 回答 1

3

您需要在 django 模板中将该代码标记为安全。https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe

dict = {{ 用户组 | 安全的}}

于 2013-04-29T09:31:45.430 回答