Reputation:
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,
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,
I guess Im looking for some direction and code tips. Am I on the right path ? Is there a better way ?
Upvotes: 9
Views: 4487
Reputation: 24260
If the information you are displaying is in a model why not just add properties/methods to the model to display whatever information you need to get out of it? Then you can pass the actual model list / query set to your template and call the methods as properties.
e.g.
class MyModel(models.Model):
model_field = models.CharField(max_length=255)
@property
def calculated_field(self):
return self._do_calculation(self.model_field)
If you need access to state variables in your loop, don't forget that you can attach any property to a python object. This can be extremely useful. So in your view you could have something like:
for row in object_list:
# update some variable we want to use in the template
row.newly_added_field = run_calculation(row, somevariable)
Both of these could then be accessed within the template:
{% for result in result_list %}
<tr>
<!-- some stuff that displays the model directly -->
<td>{{ result.calculated_field}}</td>
<td>{{ result.newly_added_field}}</td>
</tr>
{% endfor %}
Upvotes: 17