擅长:python、mysql、java
<p>发生这种情况的原因是Django模板在默认情况下将转义字符。例如,<code>&</code>替换为<code>&amp;</code>,<code>"</code>替换为<code>&quot;</code>。可以使用<a href="https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#safe" rel="nofollow noreferrer"><strong>^{<cd5>}</strong> template filter [Django-doc]</a>将内容标记为安全的。你知道吗</p>
<p>此外,最好使用<code>json.dumps</code>将数据转换为JSON blob,因此:</p>
<pre><code>import <b>json</b>
def list_test(request):
l = ['a', 'b', 'c']
context = {'l': <b>json.dumps(</b>l<b>)</b>}
return render(request, 'app/list_test.html', context)</code></pre>
<p>并将其渲染为:</p>
<pre><code><script>
let l = {{ l<b>|safe</b> }};
console.log(l);
</script<</code></pre>