python - 在 django rest 框架中返回一个生成的 HTML 文件
问题描述
我有以下视图,它接收用户传入的数据并返回一个 HTML 文件(folium 地图)。
我有三个问题:
首先,此视图在浏览器中引发错误。
TypeError at /locations/
'LocationInfo' object is not iterable
第二,如何将生成的 HTML 文件返回给用户?
第三,我希望当用户输入数据时,逻辑将运行并返回所述 HTML 文件。
我这里的业务逻辑使用用户输入的值来绘制地图并生成一个保存在目录中的 HTML 文件,我可以返回该文件的路径,或者我已经做出了另一个选项,在另一个窗口中自动打开 HTML。
# views.py
from rest_framework.viewsets import ModelViewSet
from .serializers import LocationInfoSerializer
from .models import LocationInfo
from three_Positions_plotting.app import main
def map_info_to_logic(gdt1, gdt2, uav):
html_file = main(gdt1=gdt1, gdt2=gdt2, uav=uav)
return html_file
class LocationInfoViewSet(ModelViewSet):
queryset = LocationInfo.objects.latest('date_added')
serializer_class = LocationInfoSerializer
serializer = LocationInfoSerializer(queryset, many=False)
values = list(serializer.data.values())
gdt1 = [values[1], values[2]]
gdt2 = [values[2], values[3]]
uav = [values[4], values[5]]
data = map_info_to_logic(
gdt1=gdt1,
gdt2=gdt2,
uav=uav
)
我的逻辑运行点:
import numpy as np
from Project_Level.angle_condition import MeetAngleCond
from Project_Level.plot_folium import PlotOnMap
from Project_Level.utils import convert_to_xy
from triangulationapi.three_Positions_plotting.dataframes import GetDataToGeoPandas
from triangulationapi.three_Positions_plotting.positions_data_collecting import PositionsData
def main(gdt1: list, gdt2: list, uav: list):
# Get the complete latitude, longitude, lists.
positions_data = PositionsData(gdt1=gdt1,
gdt2=gdt2,
uav=uav)
full_lat_lon_list, lat_list, lon_list = positions_data.calculate_list_lens()
# Get cartesian coordinates for every point.
gdt1_xy = np.asarray(convert_to_xy(gdt1))
gdt2_xy = np.asarray(convert_to_xy(gdt2))
# Get the angle for every point in f_lat_lon_list
angles_list = MeetAngleCond()
plot_angles_list = angles_list.create_angles_list(lat_lon_list=full_lat_lon_list,
arrayed_gdt1=gdt1_xy,
arrayed_gdt2=gdt2_xy,
uav_elev=uav[-1])
get_final_lists = GetDataToGeoPandas()
lat_lon_a, lat_lon_b, optimal = get_final_lists.create_gpd_and_final_lists(angles_list=plot_angles_list,
lat_list=lat_list,
lon_list=lon_list)
# Initialize a folium map.
plot = PlotOnMap(lat_lon_a=lat_lon_a,
lat_lon_b=lat_lon_b,
optimal=optimal)
plot.create_map(mid_point=gdt1, zoom=13)
# Plot relevant locations.
plot.plot_gdt_and_triangulation(gdt1=gdt1, gdt2=gdt2, uav=uav)
# add some plugins to the map.
plot.plugins()
# return the generated html file with the map.
html_file = plot.return_html_link()
# auto opens the file.
#auto_open = plot.auto_open(html_map_file='index.html',
# path='/home/yovel/PycharmProjects/Triangulation-#Calculator/triangulationapi/three_Positions_plotting/index'
# '.html '
# )
解决方案
您没有要显示的集合,您应该RetrieveModelMixin
使用GenericAPIView
. 例如:将行替换为以下class LocationInfoViewSet(ModelViewSet):
行:
class LocationInfoViewSet(GenericAPIView, RetrieveModelMixin):
返回的 html 应该在LocationInfoSerializer
, 作为它的字段之一。
推荐阅读
- javascript - jquery 关于属性(从加载的 html 加载)
- android - 使用多个对象和数组解析 json 数据
- php - 如何添加此 MAX 数的值
- swift - 如何通过滚动视图制作可拉伸的图像视图
- javascript - 将文本和一些基本动画放在一个圆弧中
- r - 无法使用 rtweet 和 RMeCab 标记器正确获取字符编码
- f# - 在 Visual Studio 代码中构建项目时如何指定目标体系结构?
- sql-server - 从审计表中查找更改
- java - AlwaysPretouch 是否会导致触摸 -Xms 或 -Xmx 内的页面?
- scala - 在scala中组合变量类型的函数序列