我有一个djnago rest框架RestAPI。
我的视图由用户从最后一个对象中获取数据,并由此呈现HTML文件。
我希望当用户发出发布请求时,它将自动重定向到创建的文件。
当前,我有一个手动检索方法,将id /主键字段(代码中的任务名称)添加到url行时,它将打开生成的文件。
我真的迷失了所有在线教程和文档,以及应该使用的内容,重定向到路由,装饰器,URL等...
代码:
#views.py
imports..
class PointsViewSet(ModelViewSet):
# Return all order by id, reversed.
queryset = Points.objects.all().order_by('-id')
serializer_class = PointsSerializer
data = queryset[0]
serialized_data = PointsSerializer(data, many=False)
points = list(serialized_data.data.values())
# I guess some kind of route belongs here...
def retrieve(self, request, *args, **kwargs):
mission_name = self.points[1]
assign_gdt = GeoPoint(lat=self.points[2], long=self.points[3])
gdt1 = [assign_gdt.get_lat(), assign_gdt.get_long()]
assign_uav = GeoPoint(lat=self.points[4], long=self.points[5], elevation=self.points[6])
uav = [assign_uav.get_lat(), assign_uav.get_long(), assign_uav.get_elevation()]
area_name = f"'{self.points[-2]}'"
try:
# Check if a file already exists in the DB.
HTMLFileInteractionWithDB.table = THREE_POINTS_TRINAGULATION
openfile = HTMLFileInteractionWithDB.return_file_from_db(mission_name=mission_name)
print('File was fetched from DB')
return render(request, openfile)
except:
# If no map was plotted, create a new one.
main = MainApp.run(gdt1=gdt1, uav=uav, mission_name=mission_name, area=area_name)
print('New file created')
return render(request, main)