Django 视图层传递参数到前端JS
使用safe过滤
- views.py
import json
def test(request):
data = { "key1": "value1" } # 可以是字典、数组
return render(request, "your.html", {"keyName": json.dumps(data)})
- js
<script type="text/javascript">
var data = {{ keyName|safe }} // 导入views的变量,追加‘|safe’即可
</script>