简体   繁体   English

Java脚本在尝试从django服务器设置值时出现无效语法错误

[英]Java Script giving invalid syntax error while trying to set value from django server

i am trying to set value from django server in my javaScript file 我试图在我的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>

the problem is its giving dict = [&#39;user&#39;, &#39;admin&#39;] and giving invalid syntax (due to the existence of &#39; ) any help would be highly appreciated 问题是它给予dict = [&#39;user&#39;, &#39;admin&#39;]并给出无效的语法(由于存在&#39; )任何帮助都将受到高度赞赏

You need to mark that code as safe within your django template. 您需要在django模板中将该代码标记为安全。 https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe

dict = {{ usergroup | dict = {{usergroup | safe}} 安全}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Discord Bot 在尝试启动时出现语法错误 - Discord Bot giving syntax error while trying to start 尝试在脚本标记中动态添加CDATA时出现语法错误 - Syntax error while trying to add CDATA within script tags dynamically 尝试将ID从Django模板传递给reactjs时获取错误或空值 - Get error or an empty value while trying for passing id from django template to reactjs 从 Django 模板向 JavaScript 函数传递参数时出现语法错误 - Syntax error while passing arguments to JavaScript function from Django template 为什么我的服务器在尝试设置Cookie时返回错误500? - Why is my server returning error 500 while trying to set a cookie? 语法错误代码-Java脚本 - Syntax error code - Java script python中的变量消失,语法错误 - Variable from python disappears giving error in syntax 尝试使用可选链接 (?.) 时出现语法错误 - syntax error while trying to use Optional Chaining (?.) 清除文本框,但使用Java脚本给出错误 - clear textbox but giving error using java script { &quot;message&quot;: &quot;invalid input syntax for type integer: \&quot;12 Pro\&quot;&quot; } 尝试使用 PostgreSQL 发送 POST 请求时出现以下错误 - { "message": "invalid input syntax for type integer: \"12 Pro\"" } Below error appears while trying to send POST request with PostgreSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM