adh*_*dhg 7 python django maps folium
Django 新手在这里:我的目标是将 Folium 集成到 html 页面。所以我目前所拥有的:
民意调查/views.py
def show_map(request):
#creation of map comes here + business logic
m = folium.Map([51.5, -0.25], zoom_start=10)
test = folium.Html('<b>Hello world</b>', script=True)
popup = folium.Popup(test, max_width=2650)
folium.RegularPolygonMarker(location=[51.5, -0.25], popup=popup).add_to(m)
context = {'my_map': m}
return render(request, 'polls/show_folium_map.html', context)
Run Code Online (Sandbox Code Playgroud)
民意调查/网址.py
urlpatterns = [
path('show_my_map', views.show_map, name='show_map'),
Run Code Online (Sandbox Code Playgroud)
]
和show_folium_map.html
<h1>map result comes here</h1>
{{ my_map }}
Run Code Online (Sandbox Code Playgroud)
问题是我得到了地图的“to_string”值(我向你保证我看到了)。那么我怎样才能以这样的方式集成地图,我可以实际看到地图并定义大小?
小智 8
你可以尝试下面的方法。我也遇到过同样的问题,它对我来说非常有效。
def show_map(request):
#creation of map comes here + business logic
m = folium.Map([51.5, -0.25], zoom_start=10)
test = folium.Html('<b>Hello world</b>', script=True)
popup = folium.Popup(test, max_width=2650)
folium.RegularPolygonMarker(location=[51.5, -0.25], popup=popup).add_to(m)
m=m._repr_html_() #updated
context = {'my_map': m}
return render(request, 'polls/show_folium_map.html', context)
Run Code Online (Sandbox Code Playgroud)
{{ my_map|safe }}
Run Code Online (Sandbox Code Playgroud)
为了真正将 folium 包含到自定义 django 模板中,您必须先渲染图形,然后再将其添加到上下文中(这将递归地将地图的所有部分加载到图形中)。然后在你的模板中,你必须通过调用它们的渲染函数来分别访问图形的标题、html 和脚本部分。此外,这些部分必须由 django 模板标签标记为“安全”,以便允许插入 html。请参阅下面的示例。
例子:
视图.py:
import folium
from django.views.generic import TemplateView
class FoliumView(TemplateView):
template_name = "folium_app/map.html"
def get_context_data(self, **kwargs):
figure = folium.Figure()
m = folium.Map(
location=[45.372, -121.6972],
zoom_start=12,
tiles='Stamen Terrain'
)
m.add_to(figure)
folium.Marker(
location=[45.3288, -121.6625],
popup='Mt. Hood Meadows',
icon=folium.Icon(icon='cloud')
).add_to(m)
folium.Marker(
location=[45.3311, -121.7113],
popup='Timberline Lodge',
icon=folium.Icon(color='green')
).add_to(m)
folium.Marker(
location=[45.3300, -121.6823],
popup='Some Other Location',
icon=folium.Icon(color='red', icon='info-sign')
).add_to(m)
figure.render()
return {"map": figure}
Run Code Online (Sandbox Code Playgroud)
模板/folium_app/map.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
{{map.header.render|safe}}
</head>
<body>
<div><h1>Here comes my folium map:</h1></div>
{{map.html.render|safe}}
<script>
{{map.script.render|safe}}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
您可以通过触发 的(内部)父级上的渲染来获取字符串形式的 html Map
:
m = folium.Map()
html: str = m.get_root().render()
Run Code Online (Sandbox Code Playgroud)
请注意,这会返回完整的 html 页面,因此您可能需要将其放入 iframe 中。
或者,您可以单独渲染头部、身体和脚本部分。这样你就可以将每个部分放在它所属的页面上,并且不需要 iframe:
m = folium.Map()
html_head: str = m.get_root().header.render()
html_body: str = m.get_root().html.render()
html_script: str = m.get_root().script.render()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4495 次 |
最近记录: |