This won’t be a long or complex question. I’m just wondering if there is any simple way to accomplish calculating an average of numbers pulled from the datastore.
At the moment, I have this for loop inside my HTML:
{% for rating_tmp in ratings %}
{{rating_tmp.rating}}
{% endfor %}
… and it works nicely. Every time I add a value, the for loop places it on the page in a row:
2.5 4.5 2.5 3.5 etc…
Averaging numbers is obviously the sum of the numbers over the number of values. Can I throw more python in there and accomplish this all within the HTML? Or is this something that should go into main.py?
You can probably do this in a Jinja template, but I wouldn’t recommend it. What you’re doing here is pretty clearly logic, not layout, so you should do it in code, and pass it in as a variable or a property. If you change how you average in future, for instance, you should be changing the code that generates it, not the template that displays it.