hello,
I am still working hard to learn about Django
I would like to build a dict or an object to return datas to a template.
For that I am using the render function
return render(request, 'map/station.html', {'stations': d, 'sensors': sensors, 'fields_list': fields_list, 'stations_list': stations_list})
I am actually working on the d
variable (I will give a better name later)
my goal is to have a similar structure as the following
{
"station_name":"st-1",
"station_longname":"Station 1",
"collectionid": 1524,
"date":"2022-07-16 20:00:002",
"sondes":{
"Temp":26,
"Pressure": 40 ,
"humidity":60,
},
}
i would like to have some clarification:
Do I need a dict or a an object?
And it would be very helpfully if you can give me an exemple of how to prepare it to pass in my render function
Here is a draft of what I tried to start without success
def station(request, idstation, idfield):
"""
class St():
def __init__(self, station_name, station_longname):
self.station_name = station_name
self.station_longname = station_longname
def addStationname(self, stationname):
self.station_name += stationname
stations_and_sensors = St("name","longname")
"""
d = {"station_name": "toto", "station_longname": "longname"}
for e in Sensors.objects.filter(stations_id_station=idstation, sensor_active=1):
print(e.stations_id_station.station_name)
d['station_name'] = e.stations_id_station.station_name
print(e.sensor_name)
for p in Measures.objects.filter(sensors_id_sensor=e.id_sensor).order_by('-measure_created')[:5]:
print(p.value, p.measure_created)
print(d)
return render(request, 'map/station.html', {'stations': d})
Many thankd for your help