templates for index and sensor pages

This commit is contained in:
2020-09-14 11:46:34 -05:00
parent 86cc564d27
commit 4a584cc99b
5 changed files with 60 additions and 4 deletions

View File

@@ -3,13 +3,16 @@ from django.db import models
# Create your models here.
class Sensor(models.Model):
name = models.CharField(max_length=20, default="bedroom-bme280-01")
location = models.CharField(max_length=128, default="somewhere")
temperature_unit = models.CharField(max_length=5, default="C")
pressure_unit = models.CharField(max_length=5, default="hPa")
humidity_unit = models.CharField(max_length=5, default="%")
def __str__(self):
return f"Sensor {self.name}, units are C (temperature), hPa (pressure) and % (humidity)"
return f"""Sensor {self.name}, at {self.location}, units are C
(temperature), hPa (pressure) and % (humidity)"""
class Measurement(models.Model):
sensor = models.IntegerField(default=0)
time = models.DateTimeField('timestamp', auto_now=True)
temperature = models.FloatField(default=-400)
humidity = models.FloatField(default=-1)

View File

@@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Home weather station</title>
</head>
<body>
List of sensors
{% if sensor_list %}
<ul>
{% for sensor in sensor_list %}
<li><a href="/home/sensor/{{ sensor.id }}/">{{ sensor.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No sensor is added yet.</p>
{% endif %}
</body>
</html>

View File

@@ -0,0 +1,18 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sensor details</title>
</head>
<body>
Sensor details
<ul>
<li> Name: {{ sensor.name }} </li>
<li> Location: {{ sensor.location }} </li>
<li> Temperature unit: {{ sensor.temperature_unit }} </li>
<li> Pressure unit: {{ sensor.pressure_unit }} </li>
<li> Humidity unit: {{ sensor.humidity_unit }} </li>
</ul>
</body>
</html>

View File

@@ -3,5 +3,10 @@ from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
# http://127.0.0.1:8000/home/
path('', views.index, name='homeweather_home'),
# http://127.0.0.1:8000/home/sensor/0/
path('sensor/<int:sensor>/', views.sensor, name='sensor'),
# http://127.0.0.1:8000/home/meas/0/1/
path('meas/<int:sensor>/<int:measurement>/', views.measurement, name='meas'),
]

View File

@@ -1,6 +1,16 @@
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import Sensor, Measurement
# Create your views here.
def index(request):
return HttpResponse("Home Weather")
sensor_list = Sensor.objects.order_by('id')[:5]
context = {'sensor_list': sensor_list}
return render(request, 'HomeWeather/index.html', context)
def sensor(request, sensor):
s = get_object_or_404(Sensor, pk=sensor)
return render(request, 'HomeWeather/sensor.html', {"sensor": s})
def measurement(request, sensor, measurement):
return HttpResponse(f"Sensor {sensor}, measurement {measurement}")