View .py
map = folium.Map(location=[df['latitude'].mean(),
df['longitude'].mean()],tiles="cartodbpositron",zoom_start=12)
map.save("map.html")
context = {'my_map': map}
return render(request, 'my_map.html', context)
我的 map .html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ my_map }}
</body>
浏览器结果:
folium.folium.Map object at 0x7f49d85662b0
我不确定在用户通过之前的 html 表单提交输入后如何让 html/js 在浏览器上工作... 我似乎到处都在寻找解决方案,但我无法解决任何类似的问题!
谢谢!
请您参考如下方法:
这个回复是为了增加谷歌对其他人的覆盖率,他们和我一样,在尝试在 Django 模板中渲染 Folium map 时也遇到了这个问题。
您的代码
请查看每个代码块中的注释,了解如何按预期渲染 map 。
View .py map = folium.Map(location=[df['latitude'].mean(),
df['longitude'].mean()],tiles="cartodbpositron",zoom_start=12)
map.save("map.html")
# {'my_map': map} will output the object, which is what you are seeing
# to rectify this we need to turn it into an iframe which
# the template can then render.
context = {'my_map': map} # change to {'my_map': map._repr_html_()}
return render(request, 'my_map.html', context)
map = folium.Map(location=[df['latitude'].mean(),
df['longitude'].mean()],tiles="cartodbpositron",zoom_start=12)
map.save("map.html")
# {'my_map': map} will output the object, which is what you are seeing
# to rectify this we need to turn it into an iframe which
# the template can then render.
context = {'my_map': map} # change to {'my_map': map._repr_html_()}
return render(request, 'my_map.html', context)
模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
# after making the change in our views.py this will return the html but
# the template will not render it as expected because it is being escaped.
# You must declare it 'safe' before it will be rendered correctly.
{{ my_map }} # change to {{ my_map | safe }}
</body>
有关详细信息,请参阅 Folium 文档页面 here或 this所以发帖。
希望对您有所帮助。
