【问题标题】:return a generated HTML file in django rest framework在 django rest 框架中返回一个生成的 HTML 文件
【发布时间】:2020-02-22 18:44:29
【问题描述】:

我有以下视图,它接收用户传入的数据并返回一个 HTML 文件(folium map)。

我有三个问题:

首先,此视图在浏览器中引发错误。

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

【问题讨论】:

  • 嗨 Yovel,如果您有三个单独的问题,您应该分别发布它们。此外,不要包含所有代码,而是尝试了解问题所在并仅包含最少的相关代码。

标签: python django-rest-framework django-views logic


【解决方案1】:

您没有要显示的集合,您应该使用RetrieveModelMixinGenericAPIView。例如:将class LocationInfoViewSet(ModelViewSet): 行替换为以下行:

class LocationInfoViewSet(GenericAPIView, RetrieveModelMixin):

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

【讨论】:

    猜你喜欢
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2018-06-30
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多