我试图在我的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;的存在),任何帮助将不胜感激。

最佳答案

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

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

关于javascript - Java脚本在尝试从Django服务器设置值时给出了无效的语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16274661/

10-13 02:52