简体   繁体   English

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

[英]return a generated HTML file in django rest framework

I have the following view, which takes in the data passed in by the user and returns an HTML file (folium map).我有以下视图,它接收用户传入的数据并返回一个 HTML 文件(folium 地图)。

I have three questions:我有三个问题:

firstly, this view raises an error, in the browser.首先,此视图在浏览器中引发错误。

TypeError at /locations/
'LocationInfo' object is not iterable

and second, How do I return the generated HTML file to the user?第二,如何将生成的 HTML 文件返回给用户?

third, I want that when the user inputs the data, the logic will run and return said HTML file.第三,我希望当用户输入数据时,逻辑将运行并返回 HTML 文件。

My business logic here uses the values inputted by the user to plot the map and generates an HTML file saved in the directory, I can return the path to said file, or I have made another option that opens the HTML in another window automatically.我这里的业务逻辑使用用户输入的值到 plot map 并生成一个 HTML 文件保存在目录中,我可以返回该文件的路径,或者我做了另一个选项,自动在另一个 window 中打开 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
    )

My logic running point:我的逻辑跑分:

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 '
    #                          )

You don't have a set to show, you should use the RetrieveModelMixin with the GenericAPIView .您没有要显示的集合,您应该将RetrieveModelMixinGenericAPIView一起使用。 eg: replace the line class LocationInfoViewSet(ModelViewSet): , with this line:例如:将行class LocationInfoViewSet(ModelViewSet): : 替换为以下行:

class LocationInfoViewSet(GenericAPIView, RetrieveModelMixin):

the returned html should be on the LocationInfoSerializer , as one of it's field.返回的 html 应该在LocationInfoSerializer上,作为它的字段之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM