【发布时间】:2019-11-24 17:01:50
【问题描述】:
我正在从 postgressql 数据库中读取 gps 坐标,并且正在使用 folium 创建地图。我使用 iframe 将地图嵌入 index.html 中。正在读取数据并在 index.html 中显示数据,但嵌入的 map.html 会引发错误,提示“QuerySet”对象没有“Lat”属性 - 但我的记录集确实有一个名为 Lat 的字段,我在索引中使用它.html
我在 index.html 中显示数据(纬度、经度、在这些坐标处拍摄的照片)。我创建了一个模型并在 postgressql 数据库中有数据。我在views.py 中创建了一个函数,我在其中循环遍历数据集以在folium 地图中创建标记。然后我使用 iframe 将其嵌入 index.html
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import PhotoInfo
import folium
# Create your views here.
def index(request):
VarPhotoInfo = PhotoInfo.objects.order_by('DateTaken')
context = {'PhotoInfo': VarPhotoInfo }
return render(request,'natureapp/index.html',context)
def show_map(request):
#creation of map comes here + business logic
PhotoInfo1 = PhotoInfo.objects.order_by('DateTaken')
m = folium.Map([33.571345, -117.763265], zoom_start=10)
test = folium.Html('<b>Hello world</b>', script=True)
popup = folium.Popup(test, max_width=2650)
folium.RegularPolygonMarker(location=[33.571345, -117.763265], popup=popup).add_to(m)
fg = folium.FeatureGroup(name = "MyMap")
for lt, ln, el, fn in zip(PhotoInfo1.Lat,PhotoInfo1.Lon, PhotoInfo1.DateTaken, PhotoInfo1.PhotoName):
fg.add_child(folium.Marker(location={float(lt),float(ln)},popup = str(el) +' file: '+fn, icon = folium.Icon(color='green')))
m.add_child(fg)
str = m.get_root().render()
context = {'MyMap': str}
return render(request, 'natureapp/map.html', context)
map.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>NatureMapper</title>
</head>
<h1>Map goes here </h1>
{{ my_map }}
</html>
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>NatureMapper</title>
</head>
<h1>Here it is! </h1>
{% if PhotoInfo %}
{% for Photo in PhotoInfo %}
<p>there is info.</p>
<p> {{ Photo.PhotoName }}</p>
<p> {{ Photo.Lat }}</p>
<p> {{ Photo.Long }}</p>
<p> <img src="{{ Photo.PhotoImage.url }}" width = "240" alt=""></p>
{% endfor %}
{% else %}
<p>there is no info.</p>
{% endif %}
<iframe id="encoder_iframe" height=95% width="70%" src="{% url 'show_map' %}">
</iframe>
</html>
Index.html 显示包括图片在内的所有数据。
show_map 有以下错误信息:
/map 'QuerySet' 对象的 AttributeError 没有属性 'Lat' 请求方法:GET 请求地址:http://127.0.0.1:8000/mapDjango 版本:2.2 异常类型:AttributeError 异常值: 'QuerySet' 对象没有属性 'Lat' 异常 位置:C:\Users\denjs\Documents\DjangoProjects\NatureMapper2\naturemapper2\natureapp\views.py 在 show_map 第 22 行 Python 可执行文件:C:\Users\denjs\AppData\Local\conda\conda\envs\mydjangoenv\python.exe Python 版本:3.7.2 Python 路径:
['C:\Users\denjs\Documents\DjangoProjects\NatureMapper2\naturemapper2', 'C:\Users\denjs\AppData\Local\conda\conda\envs\mydjangoenv\python37.zip', 'C:\Users\denjs\AppData\Local\conda\conda\envs\mydjangoenv\DLLs', 'C:\Users\denjs\AppData\Local\conda\conda\envs\mydjangoenv\lib', 'C:\Users\denjs\AppData\Local\conda\conda\envs\mydjangoenv', 'C:\Users\denjs\AppData\Local\conda\conda\envs\mydjangoenv\lib\site-packages', 'C:\Users\denjs\AppData\Local\conda\conda\envs\mydjangoenv\lib\site-packages\win32', 'C:\Users\denjs\AppData\Local\conda\conda\envs\mydjangoenv\lib\site-packages\win32\lib', 'C:\Users\denjs\AppData\Local\conda\conda\envs\mydjangoenv\lib\site-packages\Pythonwin'] 服务器时间:2019年7月16日星期二01:40:33 +0000
【问题讨论】: