我正在尝试将django视图中的某些数据传递给django模板,在该模板中我想将此数据保存到javascript变量中,但是会发生一些奇怪的事情。 如果我在djnago控制台中查看数据,则会看到以下内容:
[{'id': 1, 'shape': 'image', 'image': "{% static 'display_network/logos/Router.png' %}", 'label': 'Default gateway'}, {'id': 2, 'shape': 'image', 'image': "{% static 'display_network/logos/VM.png' %}", 'label': 'VM2'}, {'id': 3, 'shape': 'image', 'image': "{% static 'display_network/logos/VM.png' %}", 'label': 'VM3'}, {'id': 4, 'shape': 'image', 'image': "{% static 'display_network/logos/VM.png' %}", 'label': 'VM4'}, {'id': 5, 'shape': 'image', 'image': "{% static 'display_network/logos/VM.png' %}", 'label': 'VM5'}]
但是当我将数据保存到javascript变量并打印后,在控制台中我得到了奇怪的结果:
[{'id': 1, 'shape': 'image', 'image': "{% static 'display_network/logos/Router.png' %}", 'label': 'Default gateway'}, {'id': 2, 'shape': 'image', 'image': "{% static 'display_network/logos/VM.png' %}", 'label': 'VM2'}, {'id': 3, 'shape': 'image', 'image': "{% static 'display_network/logos/VM.png' %}", 'label': 'VM3'}, {'id': 4, 'shape': 'image', 'image': "{% static 'display_network/logos/VM.png' %}", 'label': 'VM4'}, {'id': 5, 'shape': 'image', 'image': "{% static 'display_network/logos/VM.png' %}", 'label': 'VM5'}]
我的“ js”代码:
var devices = "{{ devices }}";
console.log(devices);
所有'都转换为'
,但我不知道为什么要转换。知道如何解决这个问题吗?
答案 0 :(得分:1)
您需要将其标记为可以自动转义,然后再从字符串中进行解析。
import requests
from bs4 import BeautifulSoup
page = requests.get('https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains')
soup = BeautifulSoup(page.text, 'html.parser')
a = soup.select('table:nth-of-type(5) td:first-of-type')
b = soup.select('table:nth-of-type(6) td:first-of-type')
c = soup.select('table:nth-of-type(7) td:first-of-type')
d = soup.select('table:nth-of-type(8) td:first-of-type')
e = soup.select('table:nth-of-type(9) td:first-of-type')
f = soup.select('table:nth-of-type(10) td:first-of-type')
g = soup.select('table:nth-of-type(11) td:first-of-type')
h = soup.select('table:nth-of-type(12) td:first-of-type')
ij = soup.select('table:nth-of-type(13) td:first-of-type')
k = soup.select('table:nth-of-type(14) td:first-of-type')
l = soup.select('table:nth-of-type(15) td:first-of-type')
m = soup.select('table:nth-of-type(16) td:first-of-type')
n = soup.select('table:nth-of-type(17) td:first-of-type')
opq = soup.select('table:nth-of-type(18) td:first-of-type')
r = soup.select('table:nth-of-type(19) td:first-of-type')
s = soup.select('table:nth-of-type(20) td:first-of-type')
t = soup.select('table:nth-of-type(21) td:first-of-type')
uv = soup.select('table:nth-of-type(22) td:first-of-type')
wxyz = soup.select('table:nth-of-type(23) td:first-of-type')
print(a, b, c, d, e, f, g, h, ij, k, l, m, n, opq, r, s, t, uv, wxyz)