在Django rest框架中返回生成的HTML文件

2024-04-30 01:04:43 发布

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

我有以下视图,它接收用户传入的数据并返回一个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 '
    #                          )

Tags: tofromimportmapplothtmllistfile
1条回答
网友
1楼 · 发布于 2024-04-30 01:04:43

如果没有要显示的集合,则应将RetrieveModelMixinGenericAPIView一起使用。e、 g:将class LocationInfoViewSet(ModelViewSet): 行替换为以下行:

class LocationInfoViewSet(GenericAPIView, RetrieveModelMixin):

返回的html应该位于LocationInfoSerializer上,作为它的字段之一

相关问题 更多 >