Folium地图未显示在Django网页中 [英] Folium map not displaying in Django webpage

查看:564
本文介绍了Folium地图未显示在Django网页中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Django(和一般而言的Web开发)来说是新手,所以请提前道歉。

Very new to Django (and web development in general), so apologies in advance.

我正试图在Django网页中显示我的Folium地图,但是我似乎无法弄清楚为什么最终会出现空白屏幕。我在SO中寻找其他帖子,但他们都要求专门弹出窗口或在Jupyter中显示。

I'm trying to display my Folium map in a Django webpage, but I can't seem to figure out why I end up getting a blank screen. I looked for other posts in SO, but they're all asking for either the pop-ups specifically or displaying in Jupyter.

views.py

from django.shortcuts import render, redirect, render_to_response
from django.http import HttpResponse
from django.template.loader import get_template
from django.template.context import RequestContext
import pandas as pd
import folium

def folium_map(request):
    coords = [(40.7831, -73.9712), (40.6782, -73.9412), (40.7282, -73.7949)]
    map = folium.Map(location=[40.7118, -74.0131], zoom_start=12)
    for coord in coords:
        folium.Marker(location=[coord[0], coord[1]]).add_to(map)

    context = {'map': map}
    return render(request, 'template.html', context)

然后,在我的 template.html 文件,我只是尝试将地图插入div标签:

Then, in my template.html file, I just try to insert the map in a div tag:

<div> {{ map|safe }} </div>

它是空白。我需要iframe吗?我是否应该运行一个脚本源,以允许使用传单地图?既然需要来源,该如何设置?我是否必须将地图另存为html文件,因为我正在尝试部署此地图,以便其他人可以使用它,并且在这种情况下,每次生成地图时都必须将其保存在本地,因此'不确定人们是否会喜欢记忆或文件混乱,因为该工具会经常使用。

And it's blank. Do I need an Iframe? Is there a script source I should be running that allows for leaflet maps? How do I set it up, since I need a source? Would I have to save the map as an html file locally, because I'm trying to get this deployed so other people can use it, and if it's the case where each time the map is generated, it has to be saved locally, I'm not sure if people will appreciate having their memory or files cluttered, since this tool will be used very frequently.

我错过了一些东西来显示地图吗?我对其他映射库开放,但是Folium是迄今为止我遇到过的最简单,最互动的工具。

Am I missing something to display the map? I'm open to other mapping libraries, but Folium is by far the easiest and most interactive I've come across.

推荐答案

在您的代码中, map 是一个 folium.Map 对象,但尚未包含html / javascript字符串。您需要先渲染它。通常,这将创建一个完整的HTML页面。对于Jupyter笔记本,此HTML页面被放入iframe。因此,有两种选择:

In your code map is a folium.Map object, not yet a string with html/javascript. You need to render it first. Normally, this creates a single, full HTML page. For Jupyter notebooks this HTML page is put into an iframe. So there are two options:

如果您不想或不需要将地图放在模板中,则可以直接呈现页面: m.get_root()。render()返回带有完整HTML / JS页面的字符串。

If you don't want or need to put the map in a template, you can directly render the page: m.get_root().render() returns a string with the full HTML/JS page.

如果要将地图嵌入到可以使用iframe的模板:
m._repr_html _()返回带有HTML / JS页面的iframe的字符串。

If you want to embed the map into a template you can use an iframe: m._repr_html_() returns a string with an iframe with the HTML/JS page.

这篇关于Folium地图未显示在Django网页中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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