Django和Folium集成 [英] Django and Folium integration

查看:329
本文介绍了Django和Folium集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处是Django新手:我的目标是将Folium集成到html页面.所以我现在所拥有的:

Django newbie here: my aim is to integrate Folium to an html page. so what I have at the moment:

投票/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)

投票/urls.py

urlpatterns = [   
       path('show_my_map', views.show_map, name='show_map'),

]

show_folium_map.html

 <h1>map result comes here</h1>
 {{ my_map }}

问题是我得到了地图的"to_string"值(我保证你看到了).那么,如何集成地图以使我可以实际看到地图并定义其大小?

problem is that I get the 'to_string' value of the map (I promise you I saw that coming). So how can I integrate the map in such way that I can actually see the map and also define the size?

推荐答案

要真正将叶片包含到自定义django模板中,您必须先渲染图形,然后再将其添加到上下文中(这将以递归方式将地图的所有部分加载到该图).然后,在模板中,您必须通过调用它们的render函数来分别访问图形的标头,html和脚本部分.另外,为了允许html插入,这些部分必须由django模板标记标记为安全".参见下面的示例.

In order to really include folium into custom django template you have to render your figure first before adding it to context (This will recursivly load all parts of the map into the figure). Afterwards in your template, you have to access header, html and script parts of figure seperatly by calling their render function. Additionally, those parts have to marked as "safe" by django template tag in order to allow html insertion. See example below.

示例:

views.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}

templates/folium_app/map.html:

templates/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>

这篇关于Django和Folium集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆