【问题标题】:AttributeError at /map creating folium map in Django/map 处的 AttributeError 在 Django 中创建 folium 地图
【发布时间】: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

【问题讨论】:

    标签: python django folium


    【解决方案1】:

    查询集是对象的列表。查询集没有附加Lat 属性。

    index.html 中,您遍历了具有Lat 属性的PhotoInfo 对象的查询集。

    {% for Photo in PhotoInfo %}
       <p> {{ Photo.Lat }}</p>
    {% endif %}
    

    def show_map(request) 视图中,您没有迭代查询集,而是尝试访问查询集上的Lat 属性。

    PhotoInfo1 = PhotoInfo.objects.order_by('DateTaken')
    # PhotoInfo1 is not a single object, but rather a list of objects
    

    要么更改def show_map(request) 中的查询集以仅返回一个实例:

    PhotoInfo1 = PhotoInfo.objects.get(id=1)
    

    或者像在模板中那样迭代 PhotoInfo1 查询集。

    【讨论】:

    • 抱歉我的无知,我是在views.py中迭代吗?
    • 在这一行中:for lt, ln, el, fn in zip(PhotoInfo1.Lat,PhotoInfo1.Lon, PhotoInfo1.DateTaken, PhotoInfo1.PhotoName): PhotoInfo1 这是一个查询集,Lat 不可用。如果你想访问Lat,你需要一个对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2021-05-27
    • 2016-10-11
    • 2023-03-05
    • 2020-09-16
    • 1970-01-01
    相关资源
    最近更新 更多