Hi All
Its day two of my new life with Django, please excuse the simplicity of my question.
I have an existing DB table(read-only access) that I have successfully displayed the contents of on a webpage using urls, views, models and all that good stuff.
The challenge I have is the table does not contain all the information I need to display. The table contains test results with the columns, sampletime, samplevalue, sampleresult. I need to display different data based on what I calculate from those columns.
My end goal is to display this info as a time series graph using flotr. For now Id be happy to just dump the data I need to a table on a web page.(So I can visualize the resulting data)
What Id like to pass to the template is,
- jssampletime(the sampletime datetime object converted to javascript epoch ms)
- resultvalue(rolling sum+- of samplevalue based on whether sampleresult was good or bad)
I'm fine with creating jssampletime and resultvalue using def functions. I presume I would add these functions to views.py
I guess what I need to do iterate over the a querySet in views.py and store the results in a list of dictionaries which I pass to the template. Something like this(code not tested).
views.py
# views.py
# Sudo code to assit in asking the question
from django.shortcuts import render_to_response
from thing.reporter.models import Samples
def _datetime_to_js(sampletime):
#.. date conversion epoch magic
return jsd_result
def _rolling_sum(samplevalue,sampleresult):
#.. summing magic
return sum_result
def dumptable(request): # The def that is called by urls.py
object_list = Samples.objects.all()
list_for_template = []
for row in object_list:
jssampletime = _datetime_to_js(row.sampletime)
resultvalue = _rolling_sum(row.samplevalue,row.sampleresult)
list_for_template.append({'jssampletime':jssampletime,'resultvalue':resultvalue})
return render_to_response('tabledump.html', {'result_list': list_for_template})
tabledump.html
# tabledump.html template
{% block content %}
<h2>Results dumped to page for testing</h2>
<ul>
<table>
{% for result in result_list %}
<tr>
<td>{{ result.jssampletime }}</td>
<td>{{ result.resultvalue }}</td>
</tr>
{% endfor %}
</table>
</ul>
{% endblock %}
I think this would work but Im not sure if it is the Django MVC way.
Is it right that I,
- calculate the result I need in views.py by interating over a queryset result?
- pass my result to a template as a list of dict(is a queryset more than that)?
I guess Im looking for some direction and code tips. Am I on the right path ? Is there a better way ?