AttributeError at/map在Djang创建叶图

2024-04-19 22:48:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从postgresql数据库读取gps坐标,并使用folium创建地图。我使用iframe将地图嵌入索引.html. 数据正在中读取和显示索引.html,但是地图.html抛出一个错误,说“QuerySet”对象没有属性“Lat”-但我的记录集有一个名为Lat的字段,我在中使用它索引.html你知道吗

我显示的数据(纬度,经度,在这些坐标拍摄的照片)在索引.html. 我已经创建了一个模型,并且在postgresql数据库中有数据。我在中创建了一个函数视图.py我在数据集中循环创建一个叶状图中的标记。然后我用iframe把它嵌入到索引.html你知道吗

你知道吗视图.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>

你知道吗索引.html显示包括图片在内的所有数据。 show_map有以下错误消息:

AttributeError at /map 'QuerySet' object has no attribute 'Lat' Request Method: GET Request URL: http://127.0.0.1:8000/map Django Version: 2.2 Exception Type: AttributeError Exception Value: 'QuerySet' object has no attribute 'Lat' Exception Location: C:\Users\denjs\Documents\DjangoProjects\NatureMapper2\naturemapper2\natureapp\views.py in show_map, line 22 Python Executable: C:\Users\denjs\AppData\Local\conda\conda\envs\mydjangoenv\python.exe Python Version: 3.7.2 Python Path:
['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'] Server time: Tue, 16 Jul 2019 01:40:33 +0000


Tags: 数据mapliblocalhtmlcondausersappdata
1条回答
网友
1楼 · 发布于 2024-04-19 22:48:48

查询集是对象的列表。queryset没有附加Lat属性。你知道吗

index.html中,您迭代一组PhotoInfo对象,这些对象具有Lat属性。你知道吗

{% 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)中的queryset以仅返回单个实例:

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

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

相关问题 更多 >